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

Twitter

marți, 19 aprilie 2016

JSF Navigation Tutorial - Conditional Navigation

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

Conditional navigation allows us to specify preconditions for choosing the desired navigation case; a precondition must be met in order for the navigation case to be accepted. For this, we use the <if/> tag as a child of the <navigation-case/> tag and use an EL expression that can be evaluated to a boolean value; here the true value matches the navigation case. If you don't prefer the declarative approach, then you can simply add the conditions in action methods.

Let's see some examples of using implicit navigation. The managed bean used in the next examples is listed first and the application is named ConditionalNavigation:

@Named
@RequestScoped
public class TheBean {

 private static final Logger LOG = Logger.getLogger(TheBean.class.getName());

 private int rnd = new Random().nextInt(100);

 public int getRnd() {
  return rnd;
 }

 public String theActionWithDoneOutcome() {
  LOG.info("TheBean#theActionWithDoneOutcome() called ...");
  return rnd > 50 ? "success" : "failure";
 }
   
 public String theActionWithDoneOutcomeAndRedirect() {
  LOG.info("TheBean#theActionWithDoneOutcome() called ...");
  return rnd > 50 ? "success?faces-redirect=true" : "failure?faces-redirect=true";
 }
}

FIRE A JSF GET REQUEST AND NAVIGATE TO THE VIEW ID COMPUTED FROM THE SPECIFIED OUTCOME
JSF will interpret the outcome value of <h:link/>/<h:button/> as the targeted page name (done becomes success.xhtml/failure.xhtml via conditional navigation)

<h:link value="Click me! (h:link)" outcome="done"/>
<h:button value="Click me! (h:button)" outcome="done"/>

JSF will render the right view by evaluating the following declarative condition:

<navigation-rule>
 <from-view-id>index.xhtml</from-view-id>
 <navigation-case>
  <from-outcome>done</from-outcome>
  <if>#{theBean.rnd gt 50}</if>
  <to-view-id>/success.xhtml</to-view-id>
 </navigation-case>
 <navigation-case>
  <from-outcome>done</from-outcome>
  <if>#{theBean.rnd le 50}</if>
  <to-view-id>/failure.xhtml</to-view-id>
 </navigation-case>
</navigation-rule>

FIRE A JSF GET REQUEST. PROVIDE THE NAVIGATION OUTCOME VIA A SERVER-SIDE METHOD CALLED DURING COMPUTING THE VIEW ID (AT RENDERING TIME)
JSF will interpret the outcome value of <h:link/>/<h:button/> as the targeted page name (done returned by theActionWithDoneOutcome() becomes success.xhtml/failure.xhtml via conditional navigation)

<h:link value="Click me! (h:link)" outcome="#{theBean.theActionWithDoneOutcome()}"/>
<h:button value="Click me! (h:button)" outcome="#{theBean.theActionWithDoneOutcome()}"/>

JSF will render the right view by evaluating the following programmatic condition:

public String theActionWithDoneOutcome() {
 return rnd > 50 ? "success" : "failure";
}

FIRE (SUBMIT) A POST REQUEST VIA FORWARD MECHANISM AND NAVIGATE TO THE VIEW ID COMPUTED FROM THE SPECIFIED OUTCOME
JSF will interpret the action value of <h:commandLink/Button/> as the targeted page name (done becomes success.xhtml/failure.xhtml via conditional navigation)

<h:commandLink value="Click Me! (h:commandLink)" action="done"/>
<h:commandButton value="Click Me! (h:commandButton)" action="done"/>

JSF will render the right view by evaluating the same declarative condition from above:

<navigation-rule>
 <from-view-id>index.xhtml</from-view-id>
 <navigation-case>
  <from-outcome>done</from-outcome>
  <if>#{theBean.rnd gt 50}</if>
  <to-view-id>/success.xhtml</to-view-id>
 </navigation-case>
 <navigation-case>
  <from-outcome>done</from-outcome>
  <if>#{theBean.rnd le 50}</if>
  <to-view-id>/failure.xhtml</to-view-id>
 </navigation-case>
</navigation-rule>

FIRE (SUBMIT) A POST REQUEST VIA REDIRECT MECHANISM AND NAVIGATE TO THE VIEW ID COMPUTED FROM THE SPECIFIED OUTCOME
The presence of <redirect/> in navigation case will instruct JSF to rely on POST-redirect-GET (PRG) navigation pattern

<h:commandLink value="Click Me! (h:commandLink)" action="doneredirect"/>
<h:commandButton value="Click Me! (h:commandButton)" action="doneredirect"/>

JSF will render the right view by evaluating the following declarative condition:

<navigation-rule>
 <from-view-id>index.xhtml</from-view-id>
 <navigation-case>
  <from-outcome>doneredirect</from-outcome>
  <if>#{theBean.rnd gt 50}</if>
  <to-view-id>/success.xhtml</to-view-id>
  <redirect/>
 </navigation-case>
 <navigation-case>
  <from-outcome>doneredirect</from-outcome>
  <if>#{theBean.rnd le 50}</if>
  <to-view-id>/failure.xhtml</to-view-id>
  <redirect/>
 </navigation-case>
</navigation-rule>

FIRE (SUBMIT) A POST REQUEST VIA FORWARD MECHANISM. INVOKE AN ACTION METHOD AND NAVIGATE TO THE VIEW ID COMPUTED BASED ON THE OUTCOME RETURNED BY THIS METHOD
The action can point to an action method that returns a String. This string is considered the outcome and it will be interpreted as the targeted page name.

<h:commandLink value="Click Me! (h:commandLink)" action="#{theBean.theActionWithDoneOutcome()}"/>
<h:commandButton value="Click Me! (h:commandButton)" action="#{theBean.theActionWithDoneOutcome()}"/>           

JSF will render the right view by evaluating the following programmatic condition:

public String theActionWithDoneOutcome() {
 return rnd > 50 ? "success" : "failure";
}

FIRE (SUBMIT) A POST REQUEST VIA REDIRECT MECHANISM. INVOKE AN ACTION METHOD AND NAVIGATE TO THE VIEW ID COMPUTED BASED ON THE OUTCOME RETURNED BY THIS METHOD
The action can point to an action method that returns a String. This string is considered the outcome and it will be interpreted as the targeted page name. The presence of ?faces-redirect=true will instruct JSF to rely on POST-redirect-GET (PRG) navigation pattern.

<h:commandLink value="Click Me! (h:commandLink)" action="#{theBean.theActionWithDoneOutcomeAndRedirect()}"/>
<h:commandButton value="Click Me! (h:commandButton)" action="#{theBean.theActionWithDoneOutcomeAndRedirect()}"/>          

JSF will render the right view by evaluating the following programmatic condition:

public String theActionWithDoneOutcomeAndRedirect() {
 return rnd > 50 ? "success?faces-redirect=true" : "failure?faces-redirect=true";
}

Note
In conditional navigation, the navigation cases are evaluated even when the outcome is null or void. Notice that there is no <else/> tag or multiple conditional checking; therefore, in such cases, you have to emulate a switch statement. If you want to simply match the null outcome in any case, then you can use a condition of type: <if>#{true}</if>. Moreover, the sequence of the navigation rule affects the navigation flow; therefore, it is a good practice to prioritize conditions.

The complete application is available here.

Niciun comentariu :

Trimiteți un comentariu

JSF BOOKS COLLECTION

Postări populare

Follow by Email

Visitors Starting 4 September 2015

Locations of Site Visitors