2010-03-29 112 views
2

我需要在JSF中製作動態網頁/表單。動態:在運行時,我得到了與按鈕一起的輸入字段的數量。我會盡力向你展示我需要做什麼。動態頁面JSF

inputField1 inputField2 inputField3BUTTON1

inputField4 inputField5 BUTTON2

inputField6 inputFiled7 inputField8 inputField9 BUTTON3

我希望你明白我,我想拍的東西。所以當我點擊某些按鈕時,我必須從輸入字段收集數據並對數據進行一些操作。 如果我點擊BUTTON1我收集inputField1 inputField2 inputField3 BUTTON2 inputField4 inputField5數據等

也許我應該編程方式創建輸入字段和它們在表單組。

任何想法如何做到這一點? 有什麼想法?

回答

2

如果你有興趣在編程方式創建用戶界面與JSF,檢查該樣本項目:

Dynamic Faces

兩個詞,你可以使用綁定屬性的組件實例綁定的屬性一個支持bean:

<h:panelGroup id="helloWorldPanel" binding="#{helloWorldBean.component}"/> 

在支持bean可以填充綁定組件:

private UIComponent component; 

public UIComponent getComponent() { 
    return component; 

} 

public void setComponent(UIComponent component) { 
     final UIOutput outputText = (UIOutput) facesContext.getApplication() 
       .createComponent(HtmlOutputText.COMPONENT_TYPE); 
     outputText.setId("helloAnotherWorldMessage"); 
     component.getChildren().add(outputText); 
     outputText.setValue("Hello, Another World!"); 
     this.component = component; 
    } 

然而,這並不容易。

+0

這種方式看起來很有趣。 還有一些建議如何分組文本字段和按鈕?一些panelGrid? – Milan 2010-03-30 07:42:33

+0

是的,面板網格,面板組。檢查示例應用程序,它有一個動態構建地址編輯器的示例。 – lexicore 2010-03-30 08:50:20

+0

此綁定方法僅對JSF2有效嗎? 「Dynamic Faces」鏈接適用於JSF2 – 2010-08-18 08:28:04

3

像往常一樣(見thisthat),我的建議是不動態地添加/刪除組件。解決你的問題的另一種方式:

  • 切換可視性部件的
  • 重新綁定數據屬於組件

添加/刪除組件動態始終是禍水和機會你可以用另一種方式做得更簡單。

在你的情況,我會用一個表綁定到一個String列表。在表格的每個單元格中,我使用當前字符串呈現輸入文本框。有三個按鈕,所以有三個字符串和三個表的列表。一個按鈕的粗略草圖:

<h:dataTable value="#{bean.list}" var="item"> 
    <h:column> 
     <h:inputText value="#{item}"/> 
    </h:column> 
</h:dataTable> 
<h:commandButton action="#{bean.cmd1}" value="cmd1" /> 

這就是主意。您可以使用另一個組件,而不是在列表上迭代的表或CSS來自定義顯示並在一行上包含輸入框。

0

您可以在視圖和組輸入字段中使用控制按鈕來使用多個表單。當用戶點擊按鈕1時,只會發送輸入字段1,2,3的數據。按鈕2和按鈕3的同義詞。

<f:view> 
    <h:form id="form1"> 
    <h:inputText id="field1" value="#{backingBean.var1}"/> 
    <h:inputText id="field2" value="#{backingBean.var2}"/> 
    <h:inputText id="field3" value="#{backingBean.var3}"/> 
    <h:commandButton value="SAVE" action="#{backingBean.doButton1}"/> 
    </h:form> 
    <h:form id="form2"> 
    <h:inputText id="field4" value="#{backingBean.var4}"/> 
    <h:inputText id="field5" value="#{backingBean.var5}"/> 
    <h:commandButton value="SAVE" action="#{backingBean.doButton2}"/> 
    </h:form> 
</f:view>