Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Clean URLs in Seam: URLRewriteFilter

DZone's Guide to

Clean URLs in Seam: URLRewriteFilter

· Java Zone
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

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.

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat

Topics:

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}