Search code examples
jsfjsf-2post-redirect-get

JSF 2.0 : change default behaviour from page forward to page redirect


I know there are some advantages in using page forwarding as it is the default in JSF. I also know how to set redirect for some pages using 'faces-redirect' in the implicit navigation or the tag in the faces-config file. I searched a lot on internet but I didn't find an easy and straight way (such as a configuration parameter) to enable redirect for all pages. The reason I would like to use page redirect everywhere is the following: if every page is asked whit its url the container can be used for page authorization without the need of additional security library. Don't know if it can be a good idea, but performances are not an issue in this moment and using container security can save development time for the first release.


Solution

  • You can achieve this with a custom ConfigureableNavigationHandler. You can find a kickoff example in the answer to this question: JSF 2 and Post/Redirect/Get?

    However, I have the feeling that you're designing page-to-page navigation the wrong way. You seem to be using POST requests for simple page-to-page navigation. If you fix them to be GET requests, then you don't need to worry about this. So, use <h:link> instead of <h:commandLink> for page-to-page navigation. This way you don't need to implement the PRG. See also When should I use h:outputLink instead of h:commandLink?