Archív

Příspěvek oštítkován ‘JSF’

JSF and nice URLs - PrettyFaces

In this article I would like to introduce URL rewriting engine for JSF – PrettyFaces and provide a maven archetype for quick startup.

The motivation for using rewriting engine in web development is straightforward – nice URLs are better for SEO and easier to remember.

Integration of PrettyFaces

Integration of PrettyFaces in existing project is well described in PrettyFaces documentation. Step are following:

  1. Download PrettyFaces binary and all dependencies and put them in classpath of your project.
  2. Define PrettyFaces filter in /WEB-INF/web.xml.
  3. Create config file /WEB-INF/pretty-config.xml file.

That`s all! Now you can start to use it.

URL mapping example

<url-mapping id="viewItem">
        <pattern>/item/#{bean.selectedItem}/</pattern>
        <view-id>/pages/viewItem.jsf</view-id>
</url-mapping>

The URL mapping example above causes that any request in form /item/[anything here]/ will be served by /pages/viewItem.jsf page. Value of #{bean.selectedItem} expression will be evaluated during runtime (getter of selectedItem property of managed bean bean will be triggered).

On the other side – there is a simple way how to generate this link on JSF template in consistent way. In other words – there is only one place with rewriting rules. Once you change it there it is changed all across the project. There are two components made for this purpose:

  • pretty:link (similar as c:url but id of url mapping is used to refer the URL)
  • pretty:urlbuffer (similar as pretty:link but stores the URL in a variable instead of printing it immediatelly)
<pretty:link mappingId="viewItem">
        <f:param value="#{item}" />
        <h:outputText value="#{item}" />
</pretty:link>

There are two more <url-mapping> subelements I would like to mention:

  • <query-param name=""></query-param> – let you specify query parameter and method that will return its value.
  • <action phaseId="" onPostback=„fal­se“></action> – let you specify the bean a method that will be called during specified Phase (default phase is RESTORE_VIEW). Another parameter is onPostback that says if method should be triggered during postback.

Navigation

Instead of navigation rules stored in faces-config.xml you can use PrettyFaces to navigate. There is a special prefix pretty: you can use in method of managed beans. In case you return pretty:home view id PrettyFaces will search for url mapping with id home and redirect to this page. Or you can use just pretty: to reditect to current page.

PrettyFaces archetype

As a base for my archetype I choose Deltaset maven archetype which already includes JSF, Facelets and Richfaces. But there was one tricky problem – a dependency on ` xerces:xercesIm­pl:jar:2.4.0` which causes a SaxParser exception during Jetty start. This library was referenced from nekohtml. The solution was quite simple – exclude old version of xercesImpl and add a depencency on newer one.

Steps to create project on your computer

It is supposed you have Maven installed on your computer.

  1. Download and unzip PrettyFaces maven archetype.
  2. Install archetype into your local repository. Call mvn install in folder where you unzipped download archetype.
  3. Go to folder where you wanna create new project and call (replace [group-id] and [artifact-id] with real values)
mvn archetype:generate \
-DarchetypeGroupId=cz.itplace \
-DarchetypeArtifactId=archetype-prettyfaces \
-DarchetypeVersion=1.0-SNAPSHOT \
-DgroupId=[group-id] \
-DartifactId=[artifact-id]

Now call mvn jetty:run to start Jetty container. Or mvn eclipse:eclipse to generate an Eclipse project.

Conclusion

PrettyFaces is nice piece of code. Easy to understand and well documented.

Links

Vlastimil Vávrů Articles in english