Choosing Between Redirect and Direct Rendering

I also use an empty <redirect> element within the <navigation-case> element for the Cancel button. This is an optional element that tells JSF to send a redirect response that asks the browser to request the specified view instead of rendering it as the response to the current request.

The visible difference between a redirect response and the direct rendering of a new view is that with a redirect, the browser adjusts its address field to show the URL for the new view, but with direct rendering, the address field remains unchanged. This, in turn, affects what happens if the user reloads or bookmarks the page: with the address in the browser unchanged, reloading and bookmarking applies to the old address.

So how do you decide if you should use a redirect? To a large extent, it’s a matter of preference. I look at it like this: direct rendering is always faster, so it’s the first choice. But because the URL in the browser continues to refer to the old view even though a new view is displayed, ask yourself what happens if the user decides to reload the page or bookmark it. If doing so can cause any kind of harm (e.g., submitting an order twice) or result in unexpected behavior (e.g., using a bookmark brings up the wrong page), use a redirect instead.

Get JavaServer Faces now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.