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,这一点非常重要,因此有许多解决方案不适用于此版本。

4

1 回答 1

0

编辑:正如@Kukeltje 正确指出的那样,主要问题是值表达式不正确。更改后,以下内容适用。

您不需要“计算”复选框的值(“设置”或“取消设置”)。JSF 将简单地调用(backingbean.setValue(x)使用或),这取决于当时复选框是打开还是关闭(即,当您提交页面时)。xtruefalse

这会自动发生,因为您说value="#{backingBean.value}".

因此,setValue()您只需存储参数,getValue然后返回存储的参数。其余的由 JSF 为您完成。

如果您希望复选框默认打开,请将存储的值设置为 true。

例如:

private boolean storedValue = true;  // or false if you want it to be off by default

public boolean getValue() {
  return storedValue;
}

public void setValue(boolean value) {
  this.storedValue = value;
}
于 2017-12-28T10:48:32.177 回答