My JSF Books/Videos My JSF Tutorials OmniFaces/JSF PPTs
JSF 2.3 Tutorial
JSF Caching Tutorial
JSF Navigation Tutorial
JSF Scopes Tutorial
JSF Page Author Beginner's Guide
OmniFaces 2.3 Tutorial Examples
OmniFaces 2.2 Tutorial Examples
JSF Events Tutorial
OmniFaces Callbacks Usages
JSF State Tutorial
JSF and Design Patterns
JSF 2.3 New Features (2.3-m04)
Introduction to OmniFaces
25+ Reasons to use OmniFaces in JSF
OmniFaces Validators
OmniFaces Converters
JSF Design Patterns
Mastering OmniFaces
Reusable and less-verbose JSF code

My JSF Resources ...

Java EE Guardian
Member of JCG Program
Member MVB DZone
Blog curated on ZEEF
OmniFaces is an utility library for JSF, including PrimeFaces, RichFaces, ICEfaces ...

[OmniFaces Utilities] - Find the right JSF OmniFaces 2 utilities methods/functions

Search on blog

Petition by Java EE Guardians


marți, 19 aprilie 2016

JSF Navigation Tutorial - Preemptive Navigation

Check also:
The three golden rules of use
JSF Navigation Tutorial - Implicit Navigation
JSF Navigation Tutorial - Declarative Navigation
JSF Navigation Tutorial - Conditional Navigation
JSF Navigation Tutorial - Programmatic Navigation
JSF VS Series: Implicit Navigation VS Declarative (explicit) Navigation

Preemptive navigation is available starting with JSF 2.0. The navigation rules are more permissive and they are evaluated during the Render Response phase instead of the Invoke Application phase.

This is known as predetermined navigation or preemptive navigation. The current view ID and specified outcome are used to determine the target view ID. Afterwards, the target view ID is translated into a bookmarkable URL and used as the hyperlink's target. Practically, the URL is prepared without user interaction.

The main usage of preemptive navigation appears in bookmarkable component tags, <h:link/> and <h:button/>. For example, the following are two classical examples of preemptive navigation:

<h:link value="Success" outcome="success"/>
<h:button value="Success" outcome="success"/>

When the application starts, you can check the source code of the page to see how the corresponding URLs were mapped in the HTML tag <a/> in case of <h:link/>, and the HTML tag <input type="button"/> in case of <h:button>. Even if you never use those URLs, they are ready to serve.  Well, before JSF 2.0, navigation rules were explicitly the domain of POST requests (NavigationHandler.handleNavigation() was doing the dirty job behind the scene), but the new support for GET-based navigation and bookmarkability takes navigation to another level of flexibility and transparency (for example, the ConfigurableNavigationHandler API).

The interesting part here is how the query string of a URL is assembled. The simplest case consists of the implicit query string parameter as shown in the following code:

<h:link value="Done" outcome="done?id=done"/>.

But, you can also use <f:param/> and/or <f:viewParam/>.

Another way consists in using the <view-param/> tag nested in a <redirect/> tag in a navigation case. For example, we can add query string parameters to a redirect URL in the navigation rules. Let's create the following button:

<h:commandButton value="Success" action="#{playerBean.playerDone()}"/>

Also, a silly method named playerDone is as follows:

private String player;

public String getPlayer() {
 return player;

public void setPlayer(String player) {
 this.player = player;

public String playerDone() {
 player = "Rafael Nadal";
 return "done";

Now, we can add the player property value (of course, you can add any other value) as a parameter in the query string of the redirection navigation URL:


A URL like this will be of the format (notice how the request parameter was attached based on the navigation rule) http://host:port/app-name/faces/success.xhtml?playerparam=Rafael+Nadal.

The playerparam value will be available through the param implicit object:


The complete application is available here.

Niciun comentariu :

Trimiteți un comentariu


Postări populare

Follow by Email

Visitors Starting 4 September 2015

Locations of Site Visitors