Search code examples
jsfresourcesprettyfaces

Pretty URL hides resource using PrettyFaces


I have a webapp running on PrettyFaces 3.3.3 and Wildfly 10.1.0.Final. To make the URLs more user friendly, I used PrettyFaces. An example of a valid URL would be similar to:

http://www.example.com/en/drinks

In this case there would be two variables inside a bean:

private String language;
private String zone;

Having the values:

language = "en"
zone = "drinks"

The problem is that I also have a CSS file with the path:

http://www.example.com/styles/style.css

PrettyFaces wrongly interprets this, preventing me from accessing the real resource:

language = "styles"
zone = "style.css"

I tried to find a way to tell PrettyFaces not to translate the URL for the CSS file, but I wasn't able to find anything like that.

Is there a way to access the CSS file by keeping the pretty URL?

My current PrettyFaces config is:

<url-mapping id="zoneSelected"> 
    <pattern value="/#{navigationController.language}/#{navigationController.zone}" /> 
    <view-id>/faces/index.xhtml</view-id>
</url-mapping>

Solution

  • PrettyFaces will match any incoming request against regular expressions.

    A pattern like this:

    <pattern value="/#{bean.language}/#{bean.zone}" />
    

    Will be converted into a regular expression like this:

    /[^/]+/[^/]+
    

    That's because by default PrettyFaces uses [^/]+ for all path parameters.

    Such a general pattern will of cause also match other resources like CSS or images files.

    One way to work around this problem is to customize the regular expression pattern which PrettyFaces will use for the path parameter. This is easy to do and described here:

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

    So basically just use this pattern instead:

    <pattern value="/#{ /[a-z]{2}/ bean.language }/#{bean.zone}" />
    

    This will translate to:

    /[a-z]{2}/[^/]+
    

    In this case your style sheet won't match the pattern any more.