I've found various examples in jsf where folks have done this successfully; in these examples, the person had used to display a dynamic list of checkboxes.
I am trying to do things slightly different by building a HtmlPanelGrid with my checkboxes, then displaying that HtmlPanelGrid on the page. The checkboxes appear on the screen just like they would if I had used the path.
The problem occurs when I try to capture the values from the checkboxes to see if they were checked or not. I had initialized the 'responses' array values to 'false'. After I submit the form with a few of the checkboxes selected, I view the database and it saved all of the values as false (when there should have been a few that were 'true').
Hopefully my code snippets can help identify what I am doing wrong:
Snippet from the 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>
Snippet from 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;
}
I've been playing with all sorts of permutations, but this has been the closest I've gotten so far. It looks like at least now the group of checkboxes is being properly associated with my 'respnoses' array, but I still cannot get it to capture each individual checkbox's value into the corresponding array element.
Any suggestions and/or ideas of where I can look or start?
Thank you in advance!
Cliff