2009-06-17 51 views
2

我在我的小程序中發現了一個問題,我想知道是否有人對如何儘可能最好地解決此問題有任何提示或建議。如何根據請求範圍的bean中的值呈現表單時提交的表單提交

我有請求範圍內的bean testBean。它包含以下內容:

public class testBean { 

private boolean internal = false; 
private String input = ""; 

public String internalTrue() { 
    System.out.println("Set internal true"); 
    setInternal(true); 
    return null; 
} 
public String submitForm() { 
    System.out.println(""); 
    return null; 
} 
public boolean isInternal() { 
    return internal; 
} 
public void setInternal(boolean internal) { 
    this.internal = internal; 
} 
public String getInput() { 
    return input; 
} 
public void setInput(String input) { 
    this.input = input; 
} 

}

我的文件了welcomeJSF.jsp包含此:

<f:view> 
     <h:form> 
      <h:commandButton value="Set internal true" action="#{testBean.internalTrue}" /> 
     </h:form> 
     <h:panelGrid columns="1" rendered="#{testBean.internal}"> 
      <h:form> 
       <h:outputText value="JavaServer Faces" /><h:inputText value="#{testBean.input}" /> 
       <h:commandButton value="Go" action="#{testBean.submitForm}" /> 
      </h:form> 
     </h:panelGrid> 
    </f:view> 

當我運行應用程序林帶有按鈕 「設置內部真」。我點擊它,並且我提供了帶有「Go」按鈕的表單。點擊「Go」不會觸發我的bean中的方法,很可能是因爲該字段實際上不再在服務器上呈現,因此它不會運行該方法。有沒有什麼聰明的解決方案?

提前,謝謝你的時間。因爲在應用請求值階段其呈現屬性的結果總是以

回答

2

小組的孩子們將永遠不會解碼輸入。從安全的角度來看,這是一個明智的做法。

alt text http://www.ibm.com/developerworks/java/library/j-jsf2/basic-lifecycle.gif


有一件事你可以做的是採取的事實,即JSF組件視圖的整個使用壽命期間維持狀態的優勢。

新豆:

public class TestBean { 

    private String input = null; 
    private UIComponent panel; 

    public String internalTrue() { 
    panel.setRendered(true); 
    return null; 
    } 

    public String submitForm() { 
    panel.setRendered(false); 
    System.out.println("submitForm"); 
    return null; 
    } 

    public UIComponent getPanel() { return panel; } 
    public void setPanel(UIComponent panel) { this.panel = panel; } 

    public String getInput() { return input; } 
    public void setInput(String input) { this.input = input; } 

} 

綁定到bean的新觀點:

<f:view> 
    <h:form> 
     <h:commandButton value="Set internal true" 
     action="#{testBean.internalTrue}" /> 
    </h:form> 
    <h:panelGrid binding="#{testBean.panel}" columns="1" 
     rendered="false"> 
     <h:form> 
     <h:outputText value="JavaServer Faces" /> 
     <h:inputText value="#{testBean.input}" /> 
     <h:commandButton value="Go" action="#{testBean.submitForm}" /> 
     </h:form> 
    </h:panelGrid> 
    </f:view> 

使用的panelGrid中binding屬性將導致setPanel時調用視圖被創建/恢復。


注意,你可能有一些測試依賴於做你如何實現的和/或庫StateManager商店請求之間的意見(這又可以由javax.faces.STATE_SAVING_METHOD初始化參數的影響) 。該視圖可以存儲在窗體中的隱藏字段中,鍵值爲視圖ID的會話(可能導致與多個瀏覽器窗口發生衝突),鍵入由GET或JSF導航創建的唯一ID的會話中,或者一些完全定製的機制。框架的可插入特性使其具有通用性,但在這種情況下,這意味着您需要仔細檢查您的實現如何運行。

+0

感謝您爲我清理!非常好的答案 – 2009-06-17 21:11:19