Victor Bello
Victor Bello

Reputation: 493

Different rules for the same view-id PrettyFaces

I have a jsf page which receive between 2 and 6 parameters, I would like to make this url look better, so I started to use pretty faces. When I create the rule for 2 parameters, everything works perfectly, but when I create the second rule, the pages also works, the url is correctly like I wish but I receive error messages in the Eclipse Console. I think it's because I'm trying to create a rule for the same view-id... is that possible?

pretty-config:

  <url-mapping id="departamento">
   <pattern value="/#{codDep}/#{departamento} " />
   <view-id value="/departamento.jsf" />
  </url-mapping>

 <url-mapping parentId="departamento" id="sessao">
  <pattern value="/#{codSec}/#{secao}" />
  <view-id value="/departamento.jsf" />
 </url-mapping>

Console Error:

java.lang.NumberFormatException: For input string: "javax.faces.resource"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:492)
at java.lang.Integer.parseInt(Integer.java:527)
at project.Controller.inicializar(Controller.java:82)

Controller method:

 public void inicializar(ComponentSystemEvent event)
{
FacesContext context = FacesContext.getCurrentInstance();
if (!context.isPostback())
{

    departamento = new Departamento(Integer.parseInt(codigoDepartamento), descricaoDepartamento, null, null);
    }
  }

The problem is that It shouldn't pass this parameter "javax.faces.resource" and sometimes "css", which I don't even know where it comes from.

Upvotes: 2

Views: 1209

Answers (1)

chkal
chkal

Reputation: 5668

I guess this happens because a JSF resource request (which contains javax.faces.resource), matches one of your patterns. PrettyFaces intercepts the request and parts of the requested path are written to your beans.

You will have to change your pattern so that it only matches the URL you want to and not stuff like JSF resources, CSS or image files. You could either add some unique fixed string prefix like /dep/ like this:

<url-mapping id="departamento">
  <pattern value="/dep/#{codDep}/#{departamento} " />
  <view-id value="/departamento.jsf" />
</url-mapping>

Or restrict the values allowed for the path parameters. If codDep for example is a number, you could use this:

<url-mapping id="departamento">
  <pattern value="/#{ /[0-9]+/ codDep }/#{departamento} " />
  <view-id value="/departamento.jsf" />
</url-mapping>

See this part of the PrettyFaces documentation for details:

http://ocpsoft.org/docs/prettyfaces/3.3.3/en-US/html/Configuration.html#config.pathparams.regex

Upvotes: 1

Related Questions