Search code examples
jsfactionprettyfaces

Double call to action - prettyfaces - jsf


I am making a page in which I call a PrettyFaces page-load action method:

<url-mapping id="informes-perfil">
    <pattern value="/informes/#{informesPerfilMB.codigo}" />
    <view-id value="/faces/informes_perfil.xhtml" />
    <action onPostback="false">#{informesPerfilMB.load()}</action>
</url-mapping>

For some reason, the informesPerfilMB.load() action is called twice, and the parameter value in the second call is 'null' or 'RES_NOT_FOUND'.

Here is my load method:

public void load() {
    if (isPostBack) {
        isPostBack = false;
        try {
            System.out.println(codigo);
            informe = informeEJBServiceLocal.getByCodigo(codigo);
            this.buscarInformeIngreso();
            this.buscarInformeOtroIngreso();
        } catch (EJBServiceException e) {
            e.printStackTrace();
        }
    }
}

The isPostBack variable is initialized to false, so this should prevent the method from being called again, but for some reason it is.

This code first prints String: dcc509a6f75849b. Then when the load is repeated, it prints this: RES_NOT_FOUND

I hope this code helps explain what is happening enough to solve my problem, Thanks.


Solution

  • First, the reason your isPostBack variable is called twice is most likely because you have two instances of the bean, not one singleton instance. There are a few reasons this could be happening:

    • Your bean is request scoped and multiple requests are being made to the page.
    • Your bean is being created multiple times by parts of your application that use it and call the load() method.

    I also believe it is possible your method is being called twice because of the way you have written your EL expression (I'm not 100% sure):

     <action onPostback="false">#{informesPerfilMB.load()}</action>
                                                       ^^
    

    Note the parenthesis at the end of your method expression. I believe this will force EL to evaluate the method when the expression is evaluated. Your method expression should look like this:

     <action onPostback="false">#{informesPerfilMB.load}</action>
    

    You should also check for other places in your application where this method might be called.

    Please let me know if this helps.