Given the following simple test case.
<h:form id="form" prependId="true">
<p:commandButton value="Submit" actionListener="#{testManagedBean.action}"/>
<p:commandLink value="Submit" actionListener="#{testManagedBean.action}"/>
</h:form>
The JSF managed bean involved :
@ManagedBean
@ViewScoped
public final class TestManagedBean implements Serializable
{
private static final long serialVersionUID = 1L;
@PostConstruct
private void init() {
System.out.println("init() called");
// May invoke expensive business services like EJB calls.
}
public void action() {
System.out.println("action() called.");
}
}
When either <p:commandButton>
or <p:commandLink>
given is pressed, the init()
method is invoked in addition to the the action listener - action()
.
This should not happen, since the init()
method may have expensive business services which should not be invoked unnecessarily on every AJAX request.
Is there a way to prevent this behaviour? The init()
method should not be invoked on AJAX calls.
I'm using JSF 2.2.6 and PrimeFaces 5.0 final.