如何在支持 bean 中获取 h:selectBoolean复选框的 ID 属性



所以,这里是jsf组件:

<h:selectBooleanCheckbox id="cb#{index}" value="#{backingBean.value}" />

这是支持bean java的一部分:

/**
* getValue is a method which checks if a checkbox is selected or not, using the checkbox ID
*/
public boolean getValue() { 
//TODO: get the checkbox id
String checkboxID = ??
if (getCheckedIDs().contains(checkboxID)) {
return true;
}
return false;
}

当页面加载复选框时,我想以这种方式检查复选框是否被选中。所以问题是,写什么而不是??获取调用该方法的复选框的 ID?我只能使用 JSF 1.1 非常重要,因此有许多解决方案不适用于此版本。

另一个非常重要的事情是,我不能像这里这样在支持 bean 中使用 setter/getter:https://stackoverflow.com/a/48006066/9158590,因为我需要在选中或取消选中复选框后立即存储复选框的值,而不仅仅是在提交后。检查后我已经解决了在支持bean中的存储问题,我只需要在加载页面时发回true或false。
这是因为我使用页面导航,例如,当我选中第 1 页中的一个框,然后转到另一个页面,然后返回时,该框不再被选中(仅在支持 bean 中)。

FacesContext context = FacesContext.getCurrentInstance();
UIComponent comp = context.getViewRoot().findComponent("Parent Element 
id of HtmlSelectBooleanCheckbox ");
for(UIComponent c : comp.getChildren())
if(c instanceof HtmlSelectBooleanCheckbox)
{
// do something
}

来回答您的问题 : 变量"#{backingBean.value}"的值为真,则复选框将被选中

最新更新