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?
You can get the component tree by FacesContext#getViewRoot()
and find a particular component by ID by UIComponentBase#findComponent()
:
UIViewRoot viewRoot = FacesContext.getCurrentInstance().getViewRoot();
UIComponent component = viewRoot.findComponent("someId");
// ...
However, the view root is not directly available in the bean's constructor per se. It might return null
on 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.
<f:event type="preRenderView" listener="#{bean.init}" />
with
public void init() {
// ...
}
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).
I tried your solution and it worked :). Here i just posting my answer that how i did it. Actually someone just asked me this question that can we get all the components inside a constructor when our page invoke, i.e., why i asked on this forum:). Here is my code
/** Creates a new instance of ExporterProfileUpdateRequestGrid */
public ExporterProfileUpdateRequestGrid() {
session = ConnectionUtil.getCurrentSession();
exporterProfileUpdateRequestList = new ArrayList<ExporterProfileUpdateRequest>();
int test = selectedRadioButton;
System.out.println();
//getExporterProfileUpdateRequestGrid();
} //end of constructor
@PostConstruct
public void init() {
UIViewRoot viewRoot = FacesContext.getCurrentInstance().getViewRoot();
UIComponent component1 = viewRoot.findComponent("exporterProfileUpdateRequestGrid"); //form id
HtmlForm form = (HtmlForm)component1;
List<UIComponent> componentList = form.getChildren();
for(int i=0; i<componentList.size(); i++) {
UIComponent component = componentList.get(i);
if (component instanceof Panel) {
Panel myPanel = (Panel)component;
List<UIComponent> panelComponentList = myPanel.getChildren();
for(int j=0; j<panelComponentList.size(); j++) {
UIComponent panelComponent = panelComponentList.get(j);
System.out.println();
}
System.out.println();
}
System.out.println();
} //end of for()
UIComponent component2 = viewRoot.findComponent("panel1"); //null
UIComponent component3 = viewRoot.findComponent("myTable"); // null
} //end of init
I want to ask that i got form(HtmlForm) here, but panel1 and myTable null, why? Although i get panel and table by inspecting HtmlForm children, but why i can't get them directly.
Thanks
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With