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

1

8

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.

Llamas answered 28/6, 2012 at 10:3 Comment(0)
H
12

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

Hardner answered 28/6, 2012 at 13:2 Comment(2)
Thanks, I was puzzled because NetBeans 7.1 wizard generates exactly that kind of code (for the index.xhtml page) for page-to-page navigation using <h:commandLink> and surrounding it in an empty <form></form> structure. I removed the <form></form> and replaced the <h:commandLink> with <h:link> as per your suggestion and the behaviour is exactly the same. Not what you'd expect from a wizard.Llamas
I also never use JSF code generators. I just write it myself. I also don't use Netbeans, so I can't speak for it directly.Hardner

© 2022 - 2024 — McMap. All rights reserved.