JSF Nice URL

From MPDLMediaWiki
Revision as of 09:36, 3 February 2009 by Natasab (talk | contribs) (→‎See also)
Jump to navigation Jump to search

JSF and Nice URL[edit]

Possible solutions[edit]

Cons/Pros[edit]

Implementation[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

Installation[edit]

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 :

 <dependency>
   <groupId>urlrewrite</groupId>
   <artifactId>urlrewrite</artifactId>
   <version>3.1.0</version>
   <scope>provided</scope>
 </dependency>

In web.xml insert:

 <filter>
   <filter-name>UrlRewriteFilter</filter-name>
   <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
   <init-param>
     <param-name>logLevel</param-name>
     <param-value>WARN</param-value>
   </init-param>
 </filter>	
 <filter-mapping>
   <filter-name>UrlRewriteFilter</filter-name>
   <url-pattern>/*</url-pattern>
 </filter-mapping>

Basic use[edit]

There are 2 different ways to use urlrewrite:

1. Rules There are defined as following:

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

Simple example:

 <rule>
   <from casesensitive="false">^/home$</from>
   <to>/jsf/HomePage.jsp?</to>
  </rule>

2. Outbound-rules There are define as following:

 <outbound-rule>
   <note>
     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.
   </note>
   <from>/rewrite-status</from>
   <to>/test/status/</to>
 </outbound-rule>

Simple example:

 <outbound-rule >
   <from>jsf/HomePage.jsp</from>
   <to>home</to>
 </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.

Parameters[edit]

To define parameters, urlrewrite provide the following solution:

 <rule>
   <from casesensitive="false">^/person/([0-9]+)$</from>
   <to>/jsf/HomePage.jsp?page=1&show=12&sort1=emotion&order1=asc&person=$1</to>
 </rule>

  • 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");

Remarks[edit]

  • 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]