2011-09-03 79 views
1

我在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'数组关联,但我仍然无法将它的每个单独复选框的值捕获到相应的数组元素中。

任何建议和/或想法,我可以看看或开始?

预先感谢您!

回答

0

每当你在bean动态创建UIInputUICommand组件,您必须供应自己的组件ID,而不是让JSF来自动生成一个。

checkboxPanel.setId("someIdWhichIsUniqueWithinUIForm"); 

这样JSF就能找到提交的值。另外,你不应该在每次getter调用时重新创建组件。将创建的组件分配给一个实例变量,并添加一个if检查,该检查仅在null时才创建。

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

    return myGrid; 
} 

无关的具体问题,你认为是一个标签文件或利用rendered属性来显示/复合材料部件的隐藏不同的输入类型?以可编程方式创建组件对于可重用性和可维护性来说不是一个很好的方法。这里有一些链接应该给予一些基本思路: