1

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.

Tiny
  • 27,221
  • 105
  • 339
  • 599
  • Not happening to me in Tomcat 7 + Mojarra 2.2.6. Could you provide more info about your environment? – Aritz Jun 10 '14 at 06:36
  • Would probably be interresting to see `faces-config.xml` – Alexandre Lavoie Jun 10 '14 at 06:58
  • This is likely my first question where I cannot give any feedback. The problem was being caused somewhere else that made me think it was cased by the `@PostConstruct` method. It was fixed but I just cannot describe and in fact, it is unrelated to the example as shown in the question. – Tiny Jun 10 '14 at 08:45
  • Does your import statements consists "javax.faces.view.ViewScoped" instead of "javax.faces.bean.ViewScoped". I made this mistake and took a day to find this silly mistake :) – Srikanth Ganji Jun 11 '14 at 06:26
  • No it is `javax.faces.bean.ViewScoped`. @SrikanthGanji – Tiny Jun 12 '14 at 15:32

1 Answers1

-1

Use import javax.faces.bean.ViewScoped; if the managed bean is not a CDI artifact.

Karim Ourrai
  • 46
  • 1
  • 5