File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSF and the fly likes Best to populate components with values when page loads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "Best to populate components with values when page loads" Watch "Best to populate components with values when page loads" New topic

Best to populate components with values when page loads

Nitin Katdare

Joined: Oct 18, 2011
Posts: 6

I am new to JSF, please can any one explain how am i suppose to populate/set some properties of components(based on some criteria) on first visit of page.
I have tried to use the PhaseListener, but when page is first visited ApplyRequest values does not occur.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17257

The first thing that JSF does when asked to display a new page (View) is that the view is compiled and the "value=" expressions in the input and output controls are evaluated. Normally, those EL expressions reference properties in one or more backing beans, especially in the case of input controls. The backing bean's properties are queried to get the displayed values.

So you need to initialize the property values in the backing bean. If you do that, the display will automatically be initialized without the need to code any arcane JSF stuff - it's all POJO.

There are 2 ways to attack the particular case of setting data when a page loads. One is to invoke a setup routine in the backing bean from some other part of the application, which is a natural thing to do if the beans relate in some sort of workflow sequence. The other is to make the bean self-contained and add logic that is only called when the bean is first constructed or when the View is first requested. The @PostConstruct annotation can handle the case when you only want to initialize the bean once. Making a bean re-initialize itself on an "initial view" is trickier, but you can search this forum and find many discussions about that.

An IDE is no substitute for an Intelligent Developer.
Don't get me started about those stupid light bulbs.
subject: Best to populate components with values when page loads
It's not a secret anymore!