开发者

JSF - Multiple Choice Checkboxes not capturing responses

开发者 https://www.devze.com 2023-04-02 09:55 出处:网络
I\'ve found various examples in jsf where folks have done this successfully; in these examples, the person had usedto display a dynamic list of checkboxes.

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


Whenever you create UIInput or UICommand components dynamically in the bean, you must supply your own component ID instead of letting JSF to autogenerate one.

checkboxPanel.setId("someIdWhichIsUniqueWithinUIForm");

This way JSF will be able to find the submitted values. Also, you should not recreate the component on every getter call. Assign the created component to an instance variable and add an if check which only creates it whenever it is null.

public HtmlPanelGrid getTestSelectionGrid() {
    if (myGrid == null) {
        myGrid = new HtmlPanelGrid();
        // ...
    }

    return myGrid;
}

Unrelated to the concrete problem, have you considered a tag file or a composite component which utilizes the rendered attribute to show/hide different input types? Creating components programmatically isn't a really nice approach with regard to reusability and maintainability. Here are some links which should give some basic ideas:

  • How to make a grid of JSF composite component?
  • How to create dynamic JSF form fields
0

精彩评论

暂无评论...
验证码 换一张
取 消