Java and JBoss related stuff
RSS icon Home icon
  • Clean URLs in Seam: URLRewriteFilter

    Posted on March 31st, 2008 Adam Warski 2 comments

    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.

    Cheers,

    Adam

     

    2 responses to “Clean URLs in Seam: URLRewriteFilter” RSS icon

    • Well,

      that’s awful news, I would have thought that using UrlRewriteFilter was possible with Seam already. I’ve used it with JSF RI application a couple of years ago and it worked seamlessly (in every sense :P), with outbound news and all. Its really strange that it didn’t work, since all it needs to be done is to call response.encodeURL and presto, your rule is applied.

      One not on outbound-rules, the context path of the application does not need to be hardcoded, it can be obtained using %{context-path}.

      Regards,
      Pablo.

    • Hello,

      you’re right, using ${context-path} is much better!

      Seam encodes the URLs by its own, so whether it works with JSF RI doesn’t have much significance. Anyway, the problem was that the URL was passed to encoding before adding the parameters – see http://jira.jboss.com/jira/browse/JBSEAM-2235.

      Regards,
      Adam


    Leave a reply