3

I need to access a components tag attribute like:

<h:inputtext id="input_age"/>

from a backing bean, like:

public class UserInfo {
    String inputAgeId;
    public UserInfo() {
        inputAgeId = { /*code to access component tag attribute*/ }.getStyleClass();
    }
}
Egene
  • 183
  • 1
  • 7
ali honarmand
  • 47
  • 1
  • 2
  • 5

1 Answers1

9
UIViewRoot view = FacesContext.getCurrentInstance().getViewRoot();

You can then use view.find("component_id") to get the right component. Once you have the component, you can use getAttributes() to get a Map<String, Object> with all the attributes of the component.

If you are accessing always the same compent, you can bind it to the backing bean instead.

Community
  • 1
  • 1
Pablo
  • 3,655
  • 2
  • 30
  • 44
  • Thank you Pablo, it was helpful and also the reference was great. – ali honarmand Jun 17 '12 at 04:48
  • 1
    and also it was helpful too (http://download.oracle.com/javaee/5/tutorial/doc/bnatg.html) – ali honarmand Jun 17 '12 at 04:53
  • Is that the full ID with the prefixes of all enclosing naming containers? – Louise Sep 17 '12 at 15:27
  • This doesn't work for me. When I do getAttributes().get("class") hoping to get the class attributes of that tag I get something like *"class javax.faces.component.html.HtmlForm"*. Furhter, the map is a very special construct that hides its keys, so I don't even know what other keys I could try. – Gyro Gearloose Dec 21 '15 at 14:27
  • @GyroGearloose I would suggest you to create a new question with some code examples. – Pablo Dec 21 '15 at 14:51
  • I think I found my mistake. In jsf there is no "class" attribute but class="myClass" assignments are still rendered into the output-HTML without warning. By using name="myClass", it worked. The intend was to assign a type to some components, not a styleClass. – Gyro Gearloose Dec 21 '15 at 15:21