2011-09-03 43 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屬性來顯示/複合材料部件的隱藏不同的輸入類型?以可編程方式創建組件對於可重用性和可維護性來說不是一個很好的方法。這裏有一些鏈接應該給予一些基本思路: