【发布时间】:2011-08-21 16:24:51
【问题描述】:
我找到了一个与我想要做的非常接近的教程:http://www.andygibson.net/blog/tutorial/binding-dynamic-multi-select-checkboxes-with-jsf/
唯一的变化是我在我的支持 bean 中而不是在我的 .xhtml 文件中构建我的复选框,但是没有正确设置“值”属性。
sn-p 来自我的 .xhtml 文件:
<h:form>
<h:panelGrid binding="#{myBean.myGrid}"></h:panelGrid>
<h:commandButton id="submit" type="submit">
</h:form>
来自 myBean 的 sn-p:
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]}]"
我已经在谷歌上搜索了几个小时,我被难住了。非常感谢任何帮助或想法!
【问题讨论】:
-
我已经合并了您的注册和未注册帐户,因此您现在应该能够接受并支持 BalusC 的回答。
标签: jsf-2