Marco López
Marco López

Reputation: 156

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.

Upvotes: 1

Views: 735

Answers (2)

JSub
JSub

Reputation: 141

I've seen this happen on my similar system in the past. I think it's an interaction between faces and prettyfaces with missing files. The RES_NOT_FOUND part comes from the network traffic. There's some likely faces resource (or stylesheet) that it's trying to find in libraries and when it can't, it essentially causes the browser to go to the URL /informes/RES_NOT_FOUND. For some reason, it would often find that resource if I refreshed the page and wouldn't issue a RES_NOT_FOUND URL.

First, I'd open up the page source, and you'll find RES_NOT_FOUND, likely along with the stylesheets. Given the position of it, you might be able to correlate it to the resources loaded in your xhtml files and see which one's missing. If that doesn't help, try the developer tools and see which resources are loaded and which are not. Then make sure the resource is present, deployed, and is in the correct location.

If it's not something that you can control (like a library resource), you can always make sure your setCodigo function ignores values of "RES_NOT_FOUND".

public void setCodigo(String value) {
  if (!"RES_NOT_FOUND".equals(value)) {
    this.codigo = value;
  } 
}

You may be able to modify your security or servlet-mapping settings (in WEB.XML) to prevent URLs ending in RES_NOT_FOUND from getting to the prettyfaces pages, but I don't know enough about it to do that.

Upvotes: 2

Lincoln
Lincoln

Reputation: 3191

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.

Upvotes: 1

Related Questions