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.
1.1m 375 375 gold badges 3.6k 3.6k silver badges 3.6k 3.6k bronze badges asked Mar 15, 2010 at 23:19 21.9k 54 54 gold badges 162 162 silver badges 248 248 bronze badges Commented Mar 16, 2010 at 6:14Not sure though, there is ambiguity in the question. As long as he don't explicitly state "download file on page load", or "fire new request on page load", or so, then the mentioned topic is not necessarily a dupe of this.
Commented Mar 16, 2010 at 12:57 Correct answer is given here stackoverflow.com/a/1710413/362752 Commented Aug 2, 2012 at 14:58Just put the desired logic in the constructor of the request scoped bean associated with the JSF page.
public Bean() < // Do your stuff here. >
Use @PostConstruct annotated method on a request or view scoped bean. It will be executed after construction and initialization/setting of all managed properties and injected dependencies.
@PostConstruct public void init() < // Do your stuff here. >
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.
Alternatively, use in case you intend to initialize based on 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 @PostConstruct is perfectly fine too.
public void onload() < // Do your stuff here. >
Alternatively, use in case you intend to initialize based on 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 @PostConstruct is perfectly fine too.
public void onload() < // Do your stuff here. >
Note that this can return a String navigation case if necessary. It will be interpreted as a redirect (so you do not need a ?faces-redirect=true here).
public String onload() < // Do your stuff here. // . return "some.xhtml"; >