JSF2 URL GET parameter

It’s easily possible with JSF2 to process GET requests with this annotations:

<f:metadata>
  <f:viewParam name="id" value="#{bean.id}"/>
  <f:viewParam name="name" value="#{bean.name}"/>
  <f:event type="preRenderView" listener="#{bean.init}" />
</f:metadata>

and the init method in my bean:

public void init(ComponentSystemEvent e) throws AbortProcessingException {...}

This can be accessed with something like test.jsf?id=8012&name=name. Is it possible to use it like the @PathParam in REST and write the URL like test/8012/name?

PS: I know there is a term for this, but I cannot remember ….

Answer

The easiest way to accomplish this is by using PrettyFaces. You’ll make a pretty-config.xml file that will look like this:

<url-mapping id="test">
  <pattern value="/test/#{id}/#{name}"/>
  <view-id value="/faces/test.jsf"/>
</url-mapping>

Really, it’s dead simple. PF will inject path parameters into your managed beans and will even call an action afterwards if you like. I love it. 🙂 The official documentation is probably everything you need.

Leave a Reply

Your email address will not be published. Required fields are marked *