I am the founder and lead developer of Hibernate Envers, a Hibernate core module, which provides entity versioning/auditing capabilities. I am also one of the co-founders of SoftwareMill, a company specializing in delivering customized software solutions (http://softwaremill.com, "Extraordinary software as a standard"), based on Java and JBoss technologies. After work, apart from being involved in development of Envers, I work on several small open source projects, like ElasticMQ (simple message queue written in Scala with an SQS interface), projects around static analysis (using JSR 308 - Typestate Annotations/ Checkers Framework and FindBugs), and some CDI/Weld (not always portable) extensions, like autofactories or stackable security interceptors. I am also interested in new JVM-based languages, especially with functional elements (like Scala, JRuby) and frameworks built using them (like Lift), as well as improving the ways we use Dependency Injection. Adam is a DZone MVB and is not an employee of DZone and has posted 52 posts at DZone. You can read more from them at their website. View Full User Profile

Clean URLs in Seam: URLRewriteFilter

04.02.2008
| 8576 views |
  • submit to reddit

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:

<rule>
<from>^/index.html$</from>
<to>/home.seam</to>
</rule>

<outbound-rule>
<from>^/myapp/home.seam$</from>
<to>/myapp/</to>
</outbound-rule>

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:

<rule>
<from>^/view/(w+)$</from>
<to>/view.seam?name=$1</to>
</rule>

<rule>
<from>^/view/(w+)?cid=(d+)$</from>
<to>/view.seam?cid=$2&name=$1</to>
</rule>

<outbound-rule>
<from>^/myapp/view.seam?name=(w+)$</from>
<to>/myapp/view/$1</to>
</outbound-rule>

<outbound-rule>
<from>^/myapp/view.seam?cid=(d+)&?name=(w+)$</from>
<to>/myapp/view/$2?cid=$1</to>
</outbound-rule>

Please refer to the UrlRewriteFilter manual for further configuration details.

5
Your rating: None Average: 5 (1 vote)
Published at DZone with permission of Adam Warski, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags: