Adam Warski

31 Mar 2008

Clean URLs in Seam: URLRewriteFilter


Starting with Seam 2.0.1 (annoucement here, download here) you can fully use UrlRewriteFilter to make URLs in your Seam app nice and clean.

When using this filter, you need to define inbound-rules, which translate your pretty URLs into Seam views, for example: /myapp/view/rice –> /myapp/view.seam?name=rice.

Moreover, you can define outbound-rules, which do the translation the other way round: from Seam views into your pretty URLs. This way, you can separate the URL managing part and the Seam view files completely: if you decide to change the URLs, you just need to modify the UrlRewriteFilter configuration file, without the need to change any .xhtml files, where you can use the link-generating components (<s:link>, <s:button>) as always. (Before Seam 2.0.1, outbound rules didn’t work.)

There is one catch, however, when using the outbound-rules: you need to include the context name in them, as opposed to inboud-rules. For example, when your application is deployed in the context /myapp, to “hide” the home.seam page and make it being displayed when the user hits the root of your app (that is, /myapp or /myapp/), you’ll need the following rules:


Very often, you’ll want to translate part of the URL path to a parameter, and also include the cid parameter without changes, if it is present. Hence, the translated parameter must once be preceded by a ?, and once by a &. Here’s how you would translate the view example from the beginning:


Please refer to the UrlRewriteFilter manual for further configuration details.



comments powered by Disqus

Any questions?

Can’t find the answer you’re looking for?