我在jsf里找到了各种各样的例子,在这些例子中,这个人曾经用来显示复选框的动态列表。JSF - 多项选择复选框没有捕获响应
我想通过用我的复选框构建一个HtmlPanelGrid,然后在页面上显示该HtmlPanelGrid来做一些稍微不同的事情。如果我使用了路径,复选框就会出现在屏幕上。
当我尝试捕获复选框中的值以查看它们是否被选中时,就会出现问题。我已经将'响应'数组的值初始化为'false'。在我提交表单并选择了一些复选框后,我查看数据库,并将所有值保存为false(应该有几个“true”)。
希望我的代码片段可以帮忙鉴定一下我做错了:从displayForm.xhtml
段:
<h:form id="form_selection_test">
<!-- Display the prompt -->
<h3>
<h:outputText id="selection_prompt" value="#{testBean.prompt}"></h:outputText>
</h3>
<!-- Display the selection - the type of selection will vary based on misc criteria -->
<h:panelGrid binding="#{myBean.testSelectionGrid}">
</h:panelGrid>
<br/><br/>
<h:commandButton id="selection_form_submit" type="submit" value="Submit!" action="#{myBean.processSelection}"/>
</h:form>
段从myBean.java
public HtmlPanelGrid getTestSelectionGrid()
{
myGrid = new HtmlPanelGrid();
myGrid.setColumns(2);
List children = myGrid.getChildren();
// get application from faces context
FacesContext facesInstance = FacesContext.getCurrentInstance();
Application app = facesInstance.getApplication();
ExpressionFactory expFactory = app.getExpressionFactory();
int numChoices=choices.size(); //choices is the array containing the values for each selection option
responses.clear(); //clear any old values out of the array;
//initialize the response array (also a part of myBean) - this is supposed to capture the items the user selects
for(int initAns=0;initAns<numChoices;initAns++)
{
responses.add("false");
}
/*
*
* Other selection types (non-checkbox)
* These other selection types seem to work fine with capturing user responses
*
*/
if (selectionToDisplay =="multipleChoiceCheckbox")
{
HtmlSelectManyCheckbox checkboxPanel = new HtmlSelectManyCheckbox();
checkboxPanel.setLayout("pageDirection");
checkboxPanel.setValue("#{myBean.responses}");
Map<Object,Object> checkChoiceList = new HashMap<Object,Object>();
for (int i=0;i<numChoices;i++)
{
Object choiceValueExpression= expFactory.createValueExpression(facesInstance.getELContext(),"#{question.responses["+i+"]}",String.class).getValue(facesInstance.getELContext());
checkChoiceList.put("TEST["+i+"]"+choices.get(i),"true");//choiceValueExpression);
}
UISelectItems checkboxList = new UISelectItems();
checkboxList.setValue(checkChoiceList);
checkboxPanel.getChildren().add(checkboxList);
children.add(checkboxPanel);
}//end of if that checks if we're supposed to display MultipleChoiceCheckbox
return myGrid;
}
我一直在玩各种各样的排列,但这是迄今为止我得到的最接近的。看起来至少现在这组复选框已正确地与我的'respnoses'数组关联,但我仍然无法将它的每个单独复选框的值捕获到相应的数组元素中。
任何建议和/或想法,我可以看看或开始?
预先感谢您!
崖