Search code examples
url-rewritingprettyfaces

PrettyFaces and url-mapping with a variable ammount of path parameters


While rewriting some URLs with PrettyFaces I found myself making combinations of mappings using parent url-mappings.

For example, imagine a JSF application with a page that filters news with, say, 2 filters. While searching, the page url becomes like this

www.news.com/search.jsf?category=socials&topic=music&page=0

The problem is that category and topic are optional parameters. Am I forced to write a mapping for every combination or is there a way to do something like this (where the non existing parameters are ignored and the path parameter is not added or overlooked)?

<url-mapping id="searchResultList">
    <pattern value="/search/#{cat/}#{topic/}#{page/}"/>
    <view-id value="/pages/search.jsf"/>
</url-mapping>

Solution

  • For cases like search pagination, I don't usually recommend using path-parameters (query parameters are appropriate here IMO,) but if you still want to do something like this, then you can take one of a few potential approaches:

    1. Write a mapping for each combination. this is the most reliable approach. Parent mappings may be used to reduce complexity:

      http://ocpsoft.org/docs/prettyfaces/3.3.2/en-US/html_single/#config.mapping.parents

    2. Write a regex configured mapping and parse the dynamic parameters by hand in a custom action method:

      <url-mapping id="searchResultList">
      <pattern value="/search/#{/.*/ cat}"/>
      <view-id value="/pages/search.jsf"/>
      <action>#{urlParsingBean.parseMyURL}</action>
      </url-mapping>

    I hope this is helpful. ~Lincoln