2014-07-16 56 views
2

的狀態我已經HTML以下塊:UISelectBoolean isSelected()始終返回false無論複選框

<div data-enhance="false"> 
    <label for="qc_20012" data-iconpos="right">Text</label> 
    <div class="wrapper"> 
    <input id="qc_20012" type="checkbox" name="qc_20012"> 
    <label for="qc_20012"></label> 
    </div> 
</div> 

我只是想提取使用Java的複選框的狀態(isSelected真/假)所見如下:

//id is the id of the HTML input (in this case, qc_20012) 
UIComponent uiComponent = findComponent(id); 
if (uiComponent instanceof UISelectBoolean) { 
    UISelectBoolean comp = (UISelectBoolean) uiComponent; 
    //always prints out 'false' 
    System.out.println(comp.isSelected()); 
} 

這是的findComponent。說實話,我並不真正瞭解那裏發生了什麼,所以很可能這就是問題所在。我試着更深的挖掘,但一切似乎正確的對我說:

private UIComponent findComponent(final String id) { 
    FacesContext context = FacesContext.getCurrentInstance(); 
    UIViewRoot root = context.getViewRoot(); 
    final UIComponent[] found = new UIComponent[1]; 
    root.visitTree(new FullVisitContext(context), new VisitCallback() { 
      @Override 
      public VisitResult visit(VisitContext context, UIComponent component) { 
       if (component.getId() != null && component.getId().equals(id)) { 
        found[0] = component; 
        return VisitResult.COMPLETE; 
       } 
       return VisitResult.ACCEPT; 
      } 
     }); 
    return found[0]; 
} 

無論怎樣,comp.isSelected()行打印出來false。如果該複選框已選中,未選中或不在頁面上,則無關緊要。

下面是啓動一個結束調用該代碼鏈中的實際JSF:

<a4j:commandButton id="#{cc.attrs.id}" 
         value="#{cc.attrs.label}" 
         onclick="#{cc.attrs.onclick}" 
         data="#{cc.attrs.data}" 
         onbegin="#{cc.attrs.onbegin}" 
         onbeforedomupdate="#{cc.attrs.onbeforedomupdate}" 
         oncomplete="#{cc.attrs.oncomplete}" 
         immediate="#{cc.attrs.immediate}" 
         render="#{cc.attrs.render}" 
         execute="#{cc.attrs.execute}"/> 
+0

這看起來不像Facelets或JSF代碼。請提供JSF/Facelets代碼來複制問題。 –

+0

該項目的這一部分是在Java級別上操縱JSF。例如,javax.faces.component.UIComponentis.UISelectBoolean是我試圖操作的類。無論出於何種原因,代碼的作者都選擇了這種方法。 – Dave4988

+0

你什麼時候執行代碼來檢索'comp.isSelected()'?看起來像在應用請求值階段之前執行它。 –

回答

-1

由於您使用的CheckBox組件不是JSF組件它不是在JSF組件樹可用。所以在findComponent()方法中找不到。 selectBooleanCheckbox ID = 「qc_20012」:與

<ħ替換行

<輸入的ID = 「qc_20012」 類型= 「複選框」 名稱= 「qc_20012」/>

name =「qc_20012」/>

它會工作。