From MPDLMediaWiki
Jump to navigation Jump to search

JSF and Nice URL[edit]

Possible solutions[edit]



I tested the solution provided by : http://tuckey.org/urlrewrite/.

It works fine. I have now (in Faces) :

  • server/faces/Home instead of server/faces/faces/HomePage.jsf
  • server/faces/Details/escidoc:6423 instead of server/faces/faces/Details.jsf?item=escidoc:6423

Now we have to decide which parameters have to be written in the URL.

For Faces I propose:

  • Details view : ID of the person
  • Home page : current page, sort criterium (criteria? for R2), sort order.
  • Search Result : home page parameters + search parameters


Download the package 3.1 from : http://tuckey.org/urlrewrite/

Extract and copy the file urlrewrite.xml in your WEB-INF.

In your dependencies in the pom.xml, copy and paste :


In web.xml insert:


Basic use[edit]

There are 2 different ways to use urlrewrite:

1. Rules There are defined as following:

     The rule means that requests to /test/status/ will be redirected to /rewrite-status
     the url will be rewritten.
   <to type="redirect">%{context-path}/rewrite-status</to>

Simple example:

   <from casesensitive="false">^/home$</from>

2. Outbound-rules There are define as following:

     The outbound-rule specifies that when response.encodeURL is called (if you are using JSTL c:url)
     the url /rewrite-status will be rewritten to /test/status/.
     The above rule and this outbound-rule means that end users should never see the
     url /rewrite-status only /test/status/ both in thier location bar and in hyperlinks
     in your pages.

Simple example:

 <outbound-rule >

This 2 mechanism are two different ways to use rewriteurl. Therefore I recommend for more simplicity to use only one of this two. After a couple of try, the one which appear to me the most simple to use is the first one, the rule.


To define parameters, urlrewrite provide the following solution:

   <from casesensitive="false">^/person/([0-9]+)$</from>

  • In that example the url will look like: http://faces.mpdl.mpg.de:8080/faces/person/066
  • Parameters are define between the bracket with a regular expression. Then, to define it as parameter, just write the definition of this parameter element <to> with $1 if it is the first parameter, $2 for the second, etc.
  • You can also notice that there is the possibility to define fixed parameters values which don't appear in the url, i.e. page=1.
  • One important point is not to forget to start every rules with ^ and to close their with $. In regular expressions ^ specifies the start of the string and $ specifies the end. Thus you are sure to avoid non expected matching.

To read the parameters in your java code, proceed as following:

FacesContext context = FacesContext.getCurrentInstance();
HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
String parameterValue = request.getParameter("parameterName");


  • Apache rewrite rules might interfere with urlrewrite rules. For example in Faces, in etc/apache2/vhosts.d/faces.conf were define some rewrite rules. My own rewrite rules didn't work then from Apache and I had to call jboss directly with port 8080 to make my rules working. Be careful!
  • This page is a very small summary of what is urlrewrite. I strongly recommend to read for more information : http://urlrewritefilter.googlecode.com/svn/trunk/src/doc/manual/3.1/index.html.

See also[edit]