Jsf UISelectBoolean isSelected()始终返回false,无论复选框的状态如何
我有以下HTML块:Jsf UISelectBoolean isSelected()始终返回false,无论复选框的状态如何,jsf,Jsf,我有以下HTML块: 正文 我只想使用Java提取复选框(isSelectedtrue/false)的状态,如下所示: //id is the id of the HTML input (in this case, qc_20012) UIComponent uiComponent = findComponent(id); if (uiComponent instanceof UISelectBoolean) { UISelectBoolean comp = (UISelectBoo
正文
我只想使用Java提取复选框(isSelected
true/false)的状态,如下所示:
//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}"/>
由于您使用的复选框组件不是JSF组件,因此它在JSF组件树中不可用。因此在
findComponent()
方法中找不到它。更换线路
与
因为您使用的复选框组件不是JSF组件,所以它在JSF组件树中不可用。因此在
findComponent()
方法中找不到它。更换线路
与
这看起来根本不像Facelets或JSF代码。请提供JSF/Facelets代码来复制这个问题。例如,javax.faces.component.UIComponentis.UISelectBoolean就是我试图操纵的类。无论出于何种原因,代码的作者选择了这种方法。您何时执行代码以检索
comp.isSelected()
?看起来您是在应用请求值阶段之前执行的。它发生在通过POST提交表单之后。我不确定“应用值”阶段是什么,但我认为这将是在它之后。然而,我对JSF非常陌生,只是继承了这段代码,所以我不确定该怎么说。我知道这个组件来自RichFaces。您正在使用{cc.attrs.foo}
这一事实意味着您有一个复合组件,其中使用了这个RichFaces组件。我现在要求你们对这个问题进行更好的分析,就是这个复合组件在哪里使用,以及如何使用。这看起来根本不像Facelets或JSF代码。请提供JSF/Facelets代码来复制这个问题。例如,javax.faces.component.UIComponentis.UISelectBoolean就是我试图操纵的类。无论出于何种原因,代码的作者选择了这种方法。您何时执行代码以检索comp.isSelected()
?看起来您是在应用请求值阶段之前执行的。它发生在通过POST提交表单之后。我不确定“应用值”阶段是什么,但我认为这将是在它之后。然而,我对JSF非常陌生,只是继承了这段代码,所以我不确定该怎么说。我知道这个组件来自RichFaces。您正在使用{cc.attrs.foo}
这一事实意味着您有一个复合组件,其中使用了这个RichFaces组件。我现在要求你们对这个问题进行更好的分析,就是这个复合组件在哪里使用,以及如何使用。