One of the most common requests from JSF users is "How do I use GET requests?" or "How do I make pretty URLs?" The answer usually boiled down to implementing a custom PhaseListener to handle the URI-to-managed-bean data transfer, but these solutions were almost always very specific to the application. The RI sandbox, however, now has a general use PrettyUrlPhaseListener to handle this painlessly.

Configuring the PhaseListener is quite simple. First, the PhaseListener must be registered with JSF: Second, the PhaseListener must be configured in web.xml: The value of this context-param lists a key/value pair of actual template files and URL patterns. When the PhaseListener processes each request, it uses a regular expression created from each URL pattern to determine if the current request URL is one which it should process. If it is, the value from the request URL that corresponds to each EL expression in the pattern is extracted, URL-decoded, then set on the ValueBinding created from the EL. Once all the values have been processed, JSF is instructed to display the actual template file, specified by the key in the pair.

One more step must be performed, however. If you're pretty URLs do not match any existing servlet-mapping entries, then entries must be added until all of the URLs configured are covered. This will send the request to the FacesServlet so that the PhaseListener can process the request. The servlet-mappings for the URLs above might look like this:

With these config changes in place, the user can now request http://localhost:8080/MyApp/orderList/123456/15, and the PhaseListener will call testBean.setCustomerNumber("123456") and testBean.setPageNumber("15"), then cause JSF to display viewOrderList.jsp.