简体   繁体   中英

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

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.

Another very important thing is, that I cannot use the setter/getter in backing bean like here: https://stackoverflow.com/a/48006066/9158590 , because I need to store the value of the checkbox immediately after it's checked or unchecked, not only after submit. I have already resolved the storing in backing bean right after checking, I only need to send back true or false when loading page.
This is because I use a page navigation, and for example, when I check a box in page 1, and go to another page, and then go back, the box isn't selected anymore (only in backing 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
   }

Coming to your Question : the value of the variable "#{backingBean.value}" is true then the checkbox will be selected

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM