Search code examples
jsf-2prettyfaces

PrettyFaces url routing from form submit


I'm using Pretty Faces to do URL rewriting, in order to be able to reuse some xhtml files. I'd like to have URLs like '/honda/index.xhtml' and '/toyota/index.xhtml' both go to the same /make/index.xhtml file, with the make coming in as a parameter. This routing seems to work OK with a config like this:

<url-mapping id="carMake">
    <pattern value="/#{make}/index.xhtml"></pattern>
    <view-id value="/make/index.xhtml"/>
</url-mapping>

I also have this mapping for a search results type of page:

<url-mapping id="search">
    <pattern value="/#{make}/search/index.xhtml" />
    <view-id value="/search/index.xhtml" />
</url-mapping>

Both of these work as expected when I manually put the URL in the browser.

I run into a problem when I try to put a form on the first page, that I want to redirect to the second page. I have this jsf xhtml code for a form:

<h:form>
    <h:messages />
    <h:inputText id="searchTerm"/>
    <h:commandButton value="search" action="/honda/search/index.xhtml?faces-redirect=true"/>
</h:form>

(hard coding /honda here to simplify the example)

When I try to submit this search, it bounces back to the same /honda/index.xhtml, with no messages being displayed on the page.

The logs show this:

09-23 11:39:55 DEBUG PrettyNavigationHandler:57 - Navigation requested: fromAction [/honda/search/index.xhtml?faces-redirect=true], outcome [/honda/search/index.xhtml?faces-redirect=true]
09-23 11:39:55 DEBUG PrettyNavigationHandler:60 - Not a PrettyFaces navigation string - passing control to default nav-handler

I've tried without the faces-redirect param, but got the same result.

Why does /honda/search/index.xhtml work when I put it into the browser directly, but not as the result of an action?


Solution

  • If you want to use separate the URL from the View ID, and you don't want to reference the view-id in the app, then you need to use pretty navigation strings:

    <h:commandButton value="search" action="pretty:honda"><f:param name="make" value="honda" /></h:commandLink>

    But this really is just making it more complicated than it needs to be. I would recommend doing what @chkal suggested, except his example is a little wrong. It should have been:

    <h:commandButton value="search" action="/search/index.xhtml?faces-redirect=true&make=honda"/>

    This should be covered in the docs :) http://ocpsoft.org/docs/prettyfaces/3.3.3/en-US/html/components.html#components.prettylink Check that section (and the ones below it) and see if that helps!