0

所以,这里是 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 ,因为我需要在选中或取消选中复选框后立即存储复选框的值,不仅在提交之后。我已经在检查后解决了在backing bean中的存储,我只需要在加载页面时发回true或false。
这是因为我使用了页面导航,例如,当我在第 1 页中选中一个框,然后转到另一个页面,然后返回时,该框不再被选中(仅在 backing bean 中)。

4

1 回答 1

1
   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}”的值为真,那么复选框将被选中

于 2018-01-02T08:38:48.757 回答