You’ve already seen the navigation using Dynamic Navigation – Action Outcome, but as you’ve noted the using of navigation-case element within a faces-config.xml the from-outcome is provided. The navigation-case element is more complex that we previously discussed, In addition to the from-outcome element, there is also from-action element. That flexibility can be useful if you have two separate actions with the same outcome string.
This tutorial highlights the benefits of using the navigation with from-action element in the JSF configuration file. If you have any questions, please write it in the comments section.
Also Read:
1. Managed Bean
IndexBean.java
package net.javabeat.jsf; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; @ManagedBean @SessionScoped public class IndexBean { private String username; private String password; private String email; public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String login(){ // Do some processing if(this.username.equals("javabeat") && this.password.equals("javabeat")){ return "success"; } return "failure"; } public String notifyEmail(){ return "success"; } }
2. The Views
index.xhtml
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core"> <h:head> <h:outputScript library="javax.faces" name="jsf.js"/> </h:head> <h:body> <f:view> <h1>JavaBeat JSF 2.2 Examples</h1> <h2>JSF2 Dynamic Navigation - From Action Example</h2> <h:form prependId="false"> <h:panelGrid columns="2"> <h:outputText value="Enter Username: "/> <h:inputText value="#{indexBean.username}"/> <h:outputText value="Enter Password: "/> <h:inputText value="#{indexBean.password}"/> </h:panelGrid> <h:commandButton value="Notify Me" action="#{indexBean.notifyEmail}"></h:commandButton> <h:commandButton value="Login" action="#{indexBean.login}"></h:commandButton> </h:form> </f:view> </h:body> </html>
welcome.xhtml
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core"> <h:head> <h:outputScript library="javax.faces" name="jsf.js"/> </h:head> <h:body> <f:view> <h1>JavaBeat JSF 2.2 Examples</h1> <h2>JSF2 Dynamic Navigation - From Action Example</h2> <h:form prependId="false"> <h:outputText value="Welcome Mr. #{indexBean.username}"/> </h:form> </f:view> </h:body> </html>
relogin.xhtml
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core"> <h:head> <h:outputScript library="javax.faces" name="jsf.js"/> </h:head> <h:body> <f:view> <h1>JavaBeat JSF 2.2 Examples</h1> <h2>JSF2 Dynamic Navigation - From Action Example</h2> <h:form prependId="false"> <h:outputText value="Your Username or Password is wrong, please relogin"/> <h:commandButton value="Relogin" action="/index.xhtml"></h:commandButton> </h:form> </f:view> </h:body> </html>
notify.xhtml
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core"> <h:head> <h:outputScript library="javax.faces" name="jsf.js"/> </h:head> <h:body> <f:view> <h1>JavaBeat JSF 2.2 Examples</h1> <h2>JSF2 Dynamic Navigation - From Action Example</h2> <h:form prependId="false"> <h:outputText value="Enter Your Email: "></h:outputText> <h:inputText value="#{indexBean.email}"></h:inputText> <h:commandButton value="Any Action" action="/index.xhtml"></h:commandButton> </h:form> </f:view> </h:body> </html>
3. JSF Faces Configuration File
faces-config.xml
<?xml version="1.0" encoding="UTF-8"?> <faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd" version="2.2"> <application> <resource-bundle> <base-name>net.javabeat.jsf.application</base-name> <var>msg</var> </resource-bundle> </application> <navigation-rule> <from-view-id>/index.xhtml</from-view-id> <navigation-case> <from-action>#{indexBean.login}</from-action> <from-outcome>success</from-outcome> <to-view-id>/welcome.xhtml</to-view-id> </navigation-case> <navigation-case> <from-action>#{indexBean.notifyEmail}</from-action> <from-outcome>success</from-outcome> <to-view-id>/notify.xhtml</to-view-id> </navigation-case> <navigation-case> <from-outcome>failure</from-outcome> <to-view-id>/relogin.xhtml</to-view-id> </navigation-case> </navigation-rule> </faces-config>
- Note using of the same outcome with from within two different actions
4. The Deployment Descriptor (web.xml)
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5" metadata-complete="true"> <context-param> <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2 </description> <param-name>javax.faces.STATE_SAVING_METHOD</param-name> <param-value>client</param-value> </context-param> <context-param> <param-name>javax.faces.application.CONFIG_FILES</param-name> <param-value>/WEB-INF/faces-config.xml</param-value> </context-param> <servlet> <servlet-name>Faces Servlet</servlet-name> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>/faces/*</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>*.xhtml</url-pattern> </servlet-mapping> <listener> <listener-class>com.sun.faces.config.ConfigureListener</listener-class> </listener> </web-app>
5. JSF 2 Dynamic Navigation – From Action Demo
The below snapshots show you the using the same from-outcome to achieve different types of navigation. The login and notify actions are returned the success outcome, but they are leading into two different views. The success outcome from login action must lead us into welcome.xhtml but in case of notify action, the application must lead us into notify.xhtml for notifying the user by email.
[wpdm_file id=39]