Is there a way to execute a JSF managed bean action when a page is loaded?
If that’s relevant, I’m currently using JSF 1.2.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
JSF 1.0 / 1.1
Just put the desired logic in the constructor of the request scoped bean associated with the JSF page.
JSF 1.2 / 2.x
Use
@PostConstructannotated method on a request or view scoped bean. It will be executed after construction and initialization/setting of all managed properties and injected dependencies.This is strongly recommended over constructor in case you’re using a bean management framework which uses proxies, such as CDI, because the constructor may not be called at the times you’d expect it.
JSF 2.0 / 2.1
Alternatively, use
<f:event type="preRenderView">in case you intend to initialize based on<f:viewParam>too, or when the bean is put in a broader scope than the view scope (which in turn indicates a design problem, but that aside). Otherwise, a@PostConstructis perfectly fine too.JSF 2.2+
Alternatively, use
<f:viewAction>in case you intend to initialize based on<f:viewParam>too, or when the bean is put in a broader scope than the view scope (which in turn indicates a design problem, but that aside). Otherwise, a@PostConstructis perfectly fine too.Note that this can return a
Stringnavigation case if necessary. It will be interpreted as a redirect (so you do not need a?faces-redirect=truehere).See also:
loadevent, not during page load.