JSF Page Forward vs. Page Redirect: URL Doesn’t Refresh and Is One Step Behind
The Problem: URL Doesn’t Refresh. URL Always One Step Behind
You are working with a JSF application and seeing the URL that appears in the browser’s navigation bar is always the page you just left, rather than the page currently you are on. In other words the browser doesn’t actually refresh or reflect the actual URL that the page is attached with. The URL is always one step behind.
Behind the Scene:
As you know, navigation between application pages in a JavaServer Faces application is defined by a set of rules called Navigation rules in faces-config.xml file. See an example below.
<navigation-rule> <from-view-id> -sourcepage-</from-view-id> <navigation-case> <from-action>action-method</from-action> <from-outcome>outcome</from-outcome> <to-view-id>destination-page</to-view-id> </navigation-case> </navigation-rule>
These navigation rules determine the next page to display when a user clicks a navigation component, such as a button or a hyperlink. There are two types of page navigations in JSF. They are Page Forward and Page Redirect. By default, JSF will perform a server page forward while navigating to another page unless you specify page redirection.
In other words the JSF URLs are not used the same way as most other web application frameworks use them. These URLs are similar to session handlers and only works when submitted via POST, not GET method. This is because postback includes the JSF session information as part of the POST data stream. This is why a postback operation doesn’t return the new page’s URL in the browser toolbar. This is exactly what happens when we use Page Forward.
How to Resolve:
You can display the actual URL by forcing the client browser to make a new HTTP request for the specified view. To achieve this you can use the navigation option as part of the navigation rule.
<navigation-rule> <from-view-id> -sourcepage-</from-view-id> <navigation-case> <from-action>action-method</from-action> <from-outcome>outcome</from-outcome> <to-view-id>destination-page</to-view-id> <redirect/> </navigation-case> </navigation-rule>
The Extra Overhead When Use redirect option:
The “Page Redirect” redirect option causes the client browser to make a new HTTP request for the specified view. This is opposite to just rendering the response without requiring a separate HTTP request as in the case of “Page Forward”.
Using a redirect will terminate the current request and cause a new request – response cycle to occur. Definitely there will be a noticeable performance impact, if the page has large set of components. Also you need to understand that the extra HTTP request requires re-initialization of any request-scoped objects between pages, because it is altogether a new HTTP request. This may be achieved by storing data in session map or by using view parameters of scoped injection.
These overheads definitely should be part of your JSF application design considerations if you intend to use JSF Page Redirects.