JSF 1.1 - How to get the ID attribute of h:selectBooleanCheckbox in backing bean

烈酒焚心 提交于 2020-01-06 05:23:17

问题


So, here is the jsf component:

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

And here is a part of the backing 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;
}

When the page is loading the checkboxes, I want to check this way if the checkbox is selected or not. So the question is, what to write instead of ?? to get the ID of the checkbox who called the method? It's very important that I can use only JSF 1.1, so there are many solutions which won't work with this version.


回答1:


EDIT: as @Kukeltje correctly notes, the main issue is that the value expression is incorrect. Once you change that, the below is applicable.

You don't need to "calculate" the value ("set" or "unset") of your checkbox. JSF will simply call backingbean.setValue(x) (with x being true or false) depending on whether the checkbox is on or off at that moment (i.e. when you submit the page).

This happens automatically because you said value="#{backingBean.value}".

So in setValue() you simply store the argument, in getValue you return the stored argument. The rest is done by JSF for you.

If you want the checkbox to be on by default, you set the stored value to true.

For example:

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;
}


来源:https://stackoverflow.com/questions/48005608/jsf-1-1-how-to-get-the-id-attribute-of-hselectbooleancheckbox-in-backing-bean

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!