• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

JSF 2 Redirect Navigation Example

April 4, 2014 //  by Amr Mohammed//  Leave a Comment

You can configure the JSF configuration to redirect to a new view. Then the JSF implementation sends an HTTP redirect to the client. The redirect response tells the client which URL to use for the next page. The client then makes a GET request to that URL. Redirecting could be very slow some times because another round trip to the browser is involved. However, the redirection gives the chance to browser for updating its address field.

Without redirection, the original URL is unchanged when the user moves from the /login.xhtml page to the /welcome.xhtml page. With redirection, the browser displays the NEW URL.

For making your direction support the redirect navigation you have to compliant with the desired case below:

  • In case you don’t use of navigation rule – Through faces-config.xml – add the string ?faces-redirect=true to the outcome string.
  • In case you do use a navigation rule -Through faces-config.xml – add a redirect element after to-view-id.

The redirection facility is a JSF 2.0 feature, so jsf1.x doesn’t support like this navigation.

Also Read:

  • JSF 2 Tutorials
  • JSF Tutorials
  • Introduction to JSF

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

loginx.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 - Redirect 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 - Redirect 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 - Redirect 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:commandButton value="logout" action="login?faces-redirect=true"/>
		</h:form>
	</f:view>
</html>

3. 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>/welcome.xhtml</from-view-id>
	<navigation-case>
		<from-outcome>logout</from-outcome>
		<to-view-id>login</to-view-id>
		<redirect/>
	</navigation-case>
</navigation-rule>
</faces-config>

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 Redirection Demo

The below snapshots show you the using of redirection by using either ?faces-redirect=true appended into the outcome or add a after to-view-id.

JSF 2 Redirection Navigation Example 1

  • The user will try to login into the system using the javabeat as a username and password.
  • Once the user has activated the login action, the login method has been called and the navigation handler by its turn has handled the outcome that returned from the calling method.
  • The navigation handler should handle the next view in a non-redirect manner, cause no indicator says for it that the navigation should be redirected.

JSF 2 Redirection Navigation Example 2

  • The next view (welcome.xhtml) as been rendered after a successful login scenario.
  • Note that the address of the browser hasn’t changed, cause the navigation that made isn’t redirect one.
  • The logout that shown in the welcome.xhtml page has an associated navigation rule for it in the faces-config.xml.
  • Once the user has activated the logout action, the login.xhtml view has shown with a changed address.

JSF 2 Redirection Navigation Example 3

  • The logout action that used in the expensesView.xhtml hasn’t an associated navigation rule in the faces-config.xml file, instead of using like that navigation for achieving a redirection, you’ve already provided an inline one using ?faces-redirect=true, which will cause the same functionality.

JSF 2 Redirection Navigation Example 4

  • Both of two logout actions that introduced above (either inside welcome or expensesView) the logout action should do the same functionality plus they will cause the address to be changed.
[wpdm_file id=42]

Category: JSFTag: JSF 2

About Amr Mohammed

Previous Post: « JSF 2 Conditional Navigation Example
Next Post: JSF 2 Wildcard Navigation Rule Example »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact