Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Semantics of "?faces-redirect=true" in <commandlink action=...> and why not use it everywhere

I would like to understand what are the semantics behind appending the "?faces-redirect=true" in the action property of a <h:commandlink> tag in JSF2.0. Whether with it or with out it, the application indeed navigates to the target page specified in the action. So at first glance it seems that the only effect is cosmetic, i.e. to provide feedback to the user (if he is looking at the browser's visited URL) that he has moved to a new page. But if it is so innocuous and side-effects-free I cannot see why it is not the default behaviour. I suspect that it has to do with the post-based mechanism of JSF2.0. I 've noticed when browsing through a JSF application that the URLs one sees at his browser (when ?faces-redirect=true is not used) are the ones of the "previous" "page".

meta-NB. I am behind a firewall and plagued with the "SO requires external JavaScript from another domain" issue so I apologize for the absence of formatting. I will also provide feedback on your answers in a few hours, when I can access from another domain.

like image 583
Marcus Junius Brutus Avatar asked Jun 28 '12 10:06

Marcus Junius Brutus


1 Answers

Page-to-page navigation should not be performed using POST at all. You should be using normal <h:link> or <h:button> for this instead of <h:commandLink> or <h:commandButton>.

So instead of

<h:commandLink value="Next page" action=nextpage.xhtml?faces-redirect=true" />

you should actually be using

<h:link value="Next page" outcome="nextpage.xhtml" />

This has the major benefit that the website is now SEO friendly. Searchbots namely doesn't index forms.

Use the <h:commandLink> only if you need to submit a form with some user input. But more than often the result is just presented in the same page, if necesary conditionally rendered/included. Only on successful submits which absolutely needs to go to a different page (e.g. login/logout), you should indeed be sending a redirect. This is the so-called Post-Redirect-Get pattern.

See also

  • How to navigate in JSF? How to make URL reflect current page (and not previous one)
  • When should I use h:outputLink instead of h:commandLink?
like image 114
BalusC Avatar answered Nov 16 '22 05:11

BalusC