JSF - associating value to checkboxes built dynamically
I found a tutorial that is very close to what I am looking to do: http://www.andygibson.net/blog/tutorial/binding-dynamic-multi-select-checkboxes-with-jsf/
The only change is that I am building my checkboxes within my backing bean instead of in my .xhtml file, but the 'value' attributes are not being set correctly.
snippet from my .xhtml file:
<h:form>
<h:panelGrid binding="#{myBean.myGrid}"></h:panelGrid>
<h:commandButton id="submit" type="submit">
</h:form>
snippet from myBean:
public HtmlPanelGrid getMyGrid()
{
resultGrid = new HtmlPanelGrid();
resultGrid.setColumns(2)
List children = resultGrid.getChildren();
FacesContext myFacesInstance = FacesContext.getCurrentInstance();
Application m开发者_运维知识库yApp = 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;
}
The problem is that each checkbox's value attribute is not properly mapping to my "#{myBean.responseValue["+i+"]}" properly.
If I pull up 'View Source" from the web page, the checkbox tag's value shows up as:
value="ValueExpression[#{myBean.responseValue[0]}]"
I have been googling for hours upon hours and I am stumped. Any help or ideas are greatly appreciated!!
The 1st argument of the SelectItem
constructor expects the actual select item value, not some value expression. Fix it accordingly:
Object value = valExpression.getValue(elContext);
tempSelectItem = new SelectItem(value, choiceLabels.get(i));
精彩评论