JSF 2 Wildcard Navigation Rule Example

In JSF you can use wildcard in the from-view-id element of a navigation rule, that is the rule applies to all pages that start with the prefix /somePrefix. Only a single (*) is allowed, and it must be at the end of the ID string. If there are multiple matching wildcard rules, the longest match is taken.

The using of wildcards is typically variant, so it could be used for doing the same functionality from different views that might be spread out into the views of the application. Logout is the most common use for wildcards, that is, whatever the view you are laying on, and whatever the business you are going to execute, once you are pressing on the logout action that may located at the header, at the footer or within a popup window, the action should results in logging out of the whole application and in most probably case, the login screen has been viewed.

Using of the wildcards in conjunction of outcome has been used for making such above concept applicable. This tutorial explains the navigation using the wildcard in JSF 2 with simple example application. 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.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

@ManagedBean
@SessionScoped
public class IndexBean {
	private String username;
	private String password;
	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(){
		if(this.username.equalsIgnoreCase("javabeat") &&
				this.password.equals("javabeat")){
			return "welcome";
		}
		else {
			FacesContext.getCurrentInstance().
                                     addMessage(null,
                                 new FacesMessage("You're username or password isn't valid"));
			return "login";
		}

	}
}

2. The Views

login.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 - Wildcards Example</h2>
		<h:form prependId="false">
			<h:messages globalOnly="true" style="color:red"></h:messages>
			<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="Login" action="#{indexBean.login}"></h:commandButton>
		</h:form>
	</f:view>
</h:body>
</html>

welcome.xhtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<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">
	<f:view>
		<h:form>
			<h1>JavaBeat JSF 2.2 Examples</h1>
			<h2>JSF2 Dynamic Navigation - Wildcards Example</h2>
			<h:outputText value="Welcome Mr.#{indexBean.username}"/>
			<br/>
			<h:commandLink value="Logout" action="logout"/>
			#{' '}
			<h:commandLink value="Expenses" action="expensesView"/>
		</h:form>
	</f:view>
</html>

expensesView.xhtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<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">
	<f:view>
		<h:form>
			<h1>JavaBeat JSF 2.2 Examples</h1>
			<h2>JSF2 Dynamic Navigation - Wildcards Example</h2>
			<h:panelGrid columns="2">
				<h:outputText value="Oil"/>
				<h:outputText value="20 USD"/>
				<h:outputText value="House"/>
				<h:outputText value="50 USD"/>
				<h:outputText value="Schools"/>
				<h:outputText value="200 USD"/>
			</h:panelGrid>
			<br/>
			<h:commandLink value="logout" action="logout"/>
		</h:form>
	</f:view>
</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>/*</from-view-id>
	<navigation-case>
		<from-outcome>logout</from-outcome>
		<to-view-id>login</to-view-id>
	</navigation-case>
</navigation-rule>
</faces-config>

4. The Deployment Descriptor

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 Wildcards Navigation Demo

The below snapshots will provide you the way in that you can provide your application with a logout action that works from any view. Once the logout action has activated, and logout outcome has been acquired by the navigation handler the login screen has viewed.

JSF 2 Wildcards Navigation Example 1

  • The user has entered the username and password for passing the login screen.
  • In case the username and password isn’t valid – they’re must be javabeat – an error message will be shown.

JSF 2 Wildcards Navigation Example 2

  • Success login means the welcome page to be shown.
  • The user will see two different choices, the first one is a logout that should lead the user into login screen again – as defined the faces-config.xml – for entering the username and password again.
  • The expenses action, should navigate the user into expensesView for seeing the expenses.

JSF 2 Wildacrds Navigation Example 4

  • The logout action should lead us into login view.

JSF 2 Wildcards Navigation Example 3

  • The login screen should be viewed once the user has logged out.

Comments

comments

About Amr Mohammed

Speak Your Mind

*