I'm working in a jsf2 application and I have a problem with the page name displayed in the address bar. When I navigate to an other page I have in address bar the name of the previous page.
Thanks.
I'm working in a jsf2 application and I have a problem with the page name displayed in the address bar. When I navigate to an other page I have in address bar the name of the previous page.
Thanks.
This is happerning because, in JSF, when you are navigating to a new page the server is performing a forward to the new page. The browser is unaware of the forwarding and is displaying the old url. To make the server perform a redirect instead of forwarding, you have to either:
If you are using navigation rules, add a <redirect/>
to each rule:
<navigation-rule>
<from-view-id>/pages/from.xhtml</from-view-id>
<navigation-case>
<from-outcome>to</from-outcome>
<to-view-id>/pages/to.xhtml</to-view-id>
<redirect/>
</navigation-case>
</navigation-rule>
If you are returning an outcome from a bean method, add a ?faces-redirect=true
to the outcome:
public String navigate() {
return "/pages/to?faces-redirect=true";
}
That's because you're using POST instead of GET for page-to-page navigation and JSF defaults to submitting to the very same view with you're using <h:form>
. Using POST for page-to-page navigation is in any case a very poor approach. It's not only not user friendly (it's not bookmarkable and confusing), but it's also not SEO friendly (searchbots don't follow <form>
actions).
To solve this problem, you must stop using <h:commandLink>
for page-to-page navigation. Use <h:link>
instead.
<h:link value="Go to next page" outcome="nextpage" />
This will just render a
<a href="/contextpath/nextpage.xhtml">Go to next page</a>
Which is perfectly bookmarkable, user friendly and SEO friendly.
When you're navigating to a different page as result of a real POST form submit, I'd also replace that by returning null
or void
and just render the result/messages conditionally in the very same view. Or if the result is supposed to be bookmarkable (e.g. search results), you should use normal links or plain <form>
instead of <h:form>
and use <f:viewParam>
in target view.