2011-08-21 59 views
0

我發現了一個教程,這是非常接近我所期待的事:http://www.andygibson.net/blog/tutorial/binding-dynamic-multi-select-checkboxes-with-jsf/JSF - 關聯值複選框建立動態

唯一的變化是,我建設我支持bean中,而不是在我的複選框我.xhtml文件,但'值'屬性未被正確設置。從我.xhtml文件

片段:從爲myBean

<h:form> 
<h:panelGrid binding="#{myBean.myGrid}"></h:panelGrid> 
<h:commandButton id="submit" type="submit"> 
</h:form> 

片段:

public HtmlPanelGrid getMyGrid() 
{ 
    resultGrid = new HtmlPanelGrid(); 
    resultGrid.setColumns(2) 
    List children = resultGrid.getChildren(); 

    FacesContext myFacesInstance = FacesContext.getCurrentInstance(); 
    Application myApp = myFacesInstance.getApplication(); 
    ExpressionFactory expFactory = myApp.getExpressionFactory(); 

    SelectItem tempSelectItem; 
    String valuStringExpression; 
    ValueExpression valExpression; 

    //create panel for the checkboxes 
    HtmlSelectManyCheckbox checkboxPanel = new HtmlSelectManyCheckbox(); 
    checkboxPanel.setLayout("pageDirection"); 

    List<SelectItem> checkChoiceList = new ArrayList<SelectItem>(); 

    for (int i=0;i<numChoices;i++) 
    { 
    valStringExpression = "#{myBean.responseValue["+i+"]}"; 
valExpression = expFactory.createValueExpression(myFacesInstance.getELContext(), valStringExpression, String.class);   
    tempSelectItem = new SelectItem(valExpression,choiceLabels.get(i)); 
    checkChoiceList.add(tempSelectItem); 
    } 

    UISelectItems checkboxList = new UISelectItems(); 
    checkboxList.setValue(checkChoiceList); 
    checkboxPanel.getChildren().add(checkboxList); 

    children.add(checkboxPanel); 

return resultGrid; 
} 

的問題是,每個複選框的value屬性沒有正確映射到我的「#{myBean.responseValue [ 「+ i +」]}「正確。

如果我拉起來「查看源文件」從網頁,該複選框標籤的值顯示爲:

value="ValueExpression[#{myBean.responseValue[0]}]" 

我一直在google搜索後小時小時,我難倒任何幫助或想法。不勝感激!!

+1

我已經合併了您的註冊和未註冊的賬戶,所以您現在應該能夠接受並讚揚BalusC的回答。 – Kev

回答

0

SelectItem構造函數的第一參數預計實際選擇項目值,而不是一些值相應地表達修復它:

Object value = valExpression.getValue(elContext); 
tempSelectItem = new SelectItem(value, choiceLabels.get(i));