2011-09-03 2 views
1

사람들이 성공적으로이 작업을 수행 한 jsf에서 다양한 예제를 발견했습니다. 이 예제에서는 사람이 동적 체크 박스 목록을 표시하는 데 사용했습니다.JSF - 응답을 캡처하지 않는 다중 선택 체크 박스

내 확인란을 사용하여 HtmlPanelGrid를 작성한 다음 페이지에 해당 HtmlPanelGrid를 표시하여 약간 다른 작업을 시도하고 있습니다. 체크 박스는 경로를 사용한 것처럼 화면에 나타납니다.

확인란이 선택되었는지 확인하기 위해 값을 캡처하려고하면 문제가 발생합니다. 나는 '응답'배열 값을 'false'로 초기화했다. 체크 박스를 몇 개 선택하여 양식을 제출 한 후 데이터베이스를보고 모든 값을 false로 저장했습니다 ('사실'이 몇 개가 있어야 했음).

코드 조각을 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'배열과 적절히 연관되어있는 것처럼 보입니다. 그러나 각 개별 확인란의 값을 해당 배열 요소에 캡처 할 수는 없습니다.

어디에서 보거나 시작할 수있는 아이디어가 있습니까?

미리 감사드립니다. 대신 JSF를 댄다
클리프 당신이 빈에서 동적으로 UIInput 또는 UICommand 구성 요소를 만들 때마다

답변

0

, 당신 해야 공급 자신의 구성 요소 ID는 하나를 자동 생성합니다.

checkboxPanel.setId("someIdWhichIsUniqueWithinUIForm"); 

이렇게하면 JSF는 제출 된 값을 찾을 수 있습니다. 또한 모든 getter 호출시 구성 요소를 다시 작성하지 않아야합니다. 생성 된 구성 요소를 인스턴스 변수에 할당하고 null 일 때만 생성하는 if 검사를 추가하십시오. 구체적인 문제에

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

    return myGrid; 
} 

관련없는 , 당신은 태그 파일이나/보여주기 위해 rendered 속성을 이용하여 복합 구성 요소가 다른 입력 유형을 숨길 생각했다? 프로그래밍 방식으로 구성 요소를 작성하는 것은 재사용 및 유지 관리와 관련하여 정말 좋은 접근 방법이 아닙니다.

: 여기에 몇 가지 기본적인 아이디어를 제공 할 몇 가지 링크가 있습니다