Imagine a JSF page with several components such as <h:selectOneMenu>, <h:dataTable>, <h:panelGrid>, etc. Each component has an ID. Is there any method or technique by which I can get the components programmatically when the constructor of the bean is invoked?
Imagine a JSF page with several components such as <h:selectOneMenu> , <h:dataTable> , <h:panelGrid>
Share
You can get the component tree by
FacesContext#getViewRoot()and find a particular component by ID byUIComponentBase#findComponent():However, the view root is not directly available in the bean’s constructor per se. It might return
nullon GET requests to a view wherein the bean is not referenced in a view build time tag or attribute. It’s however guaranteed to be available during the pre render view event.with
Unrelated to the concrete problem, it’s not clear why you need to do this, but I can tell that this is more than often a code smell. I suggest to investigate if this is really the right solution for the concrete functional requirement you’ve had in mind. For clues and hints, see also How does the 'binding' attribute work in JSF? When and how should it be used? and How to use component binding in JSF right ? (request-scoped component in session scoped bean).