In case you'd like to do your job in a @PostConstruct
method you will find the following method useful. It basically initializes data only when view is loaded initially and skips initialization on postbacks:
@PostConstruct
public void initialize() {
if (!FacesContext.getCurrentInstance().isPostback()) {
//load your data
}
}
Or sometimes we may use JSF event listener. It allows to initialize your data just before the view is to be rendered:
<f:event type="preRenderView" listener="#{bean.initialize}" />
with the same method, but without @PostConstruct
annotation:
public void initialize() {
if (!FacesContext.getCurrentInstance().isPostback()) {
//load your data
}
}
Finally, when JSF 2.2 is out you could use <f:viewAction>
instead of <f:event>
with the same method, but without the check, like:
<f:viewAction action="#{bean.initialize}" onPostback="false" />
with
public void initialize() {
//load your data
}
But in case you're using PrettyFaces, just stick to the answer by kolossus. But just in case in won't like to use PrettyFaces for the job, the three methods above are always there for you. Of course, they'll work with 'plain' JSF.