Over a million developers have joined DZone.

Clean URLs in Seam: URLRewriteFilter

· Java Zone

Discover how AppDynamics steps in to upgrade your performance game and prevent your enterprise from these top 10 Java performance problems, brought to you in partnership with AppDynamics.

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.

The Java Zone is brought to you in partnership with AppDynamics. AppDynamics helps you gain the fundamentals behind application performance, and implement best practices so you can proactively analyze and act on performance problems as they arise, and more specifically with your Java applications. Start a Free Trial.

Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}