2012-01-05 6 views
1

「home.jsp」という名前のJSPページがあります。 そして私は "getRunHomeDisplay"という名前のメソッドを持っています。ページロード時にメソッドを実行するjsp

最初は私がhome.jspにボタンを持っています。そのボタンをクリックすると、家の中のすべてのアイテムが表示されます。

これは、ボタンの呼び出しです:

try { 
      log("Account selected: "); 
      (new action.ItemShowAction()).execute(getgetRunHomeDisplay()); 
      } catch (Exception e) { 

      return "error"; 
      } 
      return "success"; 

これが私の「home.jspを」ソースです:

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
     <%-- jsf:pagecode language="java" location="/src/pagecode/Itemtest2.java" --%>       <%-- /jsf:pagecode --%><%@page 
language="java" contentType="text/html; charset=ISO-8859-1" 
pageEncoding="ISO-8859-1"%> 
<%@taglib uri="http://java.sun.com/jsf/core" prefix="f"%> 
<%@taglib uri="http://www.ibm.com/jsf/html_extended" prefix="hx"%> 
<%@taglib uri="http://java.sun.com/jsf/html" prefix="h"%> 
<html> 
<head> 
    <title>itemtest2</title> 
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
    <link rel="stylesheet" type="text/css" title="Style" 
href="theme/stylesheet.css"> 
    <script type="text/javascript"> 
</script></head> 
<script type="text/javascript"> 

</script> 
<f:view> 
<body onload="#{pc_ItemClick.doButton1Action}"> 
<hx:scriptCollector id="scriptCollector1"> 
    <h:form styleClass="form" id="form1"> 
     <hx:commandExButton type="submit" value="Submit" 
      styleClass="commandExButton" id="button1" 
      action="#{pc_ItemClick.doButton1Action}"></hx:commandExButton> 
     <hx:dataTableEx id="item1" value="#{itemtest.item}" var="varitem" 
      styleClass="dataTableEx" headerClass="headerClass" 
      footerClass="footerClass" rowClasses="rowClass1, rowClass2" 
      columnClasses="columnClass1" border="0" cellpadding="2" 
      cellspacing="0"> 
      <hx:columnEx id="item2column"> 
       <f:facet name="header"> 
        <h:outputText styleClass="outputText" value="Item" id="item2text"></h:outputText> 
       </f:facet> 
       <h:outputText styleClass="outputText" id="item2" value="#{varitem}"> 
       </h:outputText> 
      </hx:columnEx> 
     </hx:dataTableEx>```1 
    </h:form> 
</hx:scriptCollector> 
</body> 
</f:view> 
</html> 

どのように私が代わりにクリックするのページのロード時に、この方法を実行するために呼び出す必要がありますボタン?

答えて

0

あなたはManagedBeanがそのJSPページに結びついていると思います。それが本当であれば、@PostConstruct注釈を利用できると思います。ページが要求されたときにこのprepareView関数が常に呼び出されます

@ManagedBean 
@RequestScoped 
public class MrBean{ 
    @PostConstruct 
    public void prepareView() { 
     // do what you need to do 
    } 
} 

:それはこのようなものになるだろう。

+0

作成された管理対象のBeanは、managedbeanですか? –

+0

そうだと思います。ちょっとしたテストをして、動作するかどうかを確認します:P –

+0

OPはJSF 2.xを使用していないようです。 – BalusC

2

IBM Faces Client FrameworkでJSF 1.xを使用しているようです。 JSF 1.0または1.1の場合は、Beanのコンストラクタでジョブを実行します。本当に他のクリーンな方法はありません。

public class ItemClick { 

    public ItemClick() { 
     // Do your job here. 
    } 

    // ... 
} 

それともは、Java EE 5にJSF 1.2を使用している場合、あなたはまた、@PostConstructアノテーションでランダムメソッドに注釈を付けることができます。 Beanの構築の直後に呼び出されます。これは、管理プロパティや注入されたEJBに応じてジョブを実行している場合に、より便利です。

public class ItemClick { 

    @PostConstruct 
    public void init() { 
     // Do your job here. 
    } 

    // ... 
} 

両方の方法で、すべてのページリクエストで実行するには、Beanがリクエストスコープであることが必要です。セッションスコープBeanは、すべてのページリクエストでは再構築されませんが、セッション中の最初のページリクエストでは1回だけ再構築されます。

「目に見えない」フォームを作成してJavaScriptで送信するのが面倒な方法です。

<body onload="document.getElementById('formId:buttonId').click()"> 
    <h:form id="formId" style="display: none;"> 
     <h:commandButton id="buttonId" value="submit" action="#{pc_ItemClick.doAction}" /> 
    </h:form> 
    ... 
</body> 

不利な点は、同じページに戻るとページが更新され、無限ループに陥ることがあります。別のページに移動したり、アクションが既に呼び出されているときにonload関数をスキップするJS条件を追加することができます。次のようなものがあります。

<body onload="<h:outputText value="document.getElementById('formId:buttonId').click()" rendered="#{!pc_ItemClick.didAction}" />"> 
+0

@PostConstructを配置しましたが、タイプが –

+0

に解決できないというエラーがあります。次に、Java EE 5でJSF 1.2を使用せず、古いJSF 1.1または1.0を使用しています。代わりにBeanのコンストラクタでジョブを実行するか、隠しフォームのハックを使用します。 – BalusC