logo
Home Article

Using JavaServer Faces (JSF) Navigation Handler Decorator

Returning back to the original page after login


URL: http://www.exadel.com/downloads/jsf/examples/jsf-loginRedirect.war (1.7M)

This short example shows how to use the Javaserver Faces Navigation Handler decorator to extend the standard navigation mechanism used in the JSF. The application has a login page and two pages that allow to show and edit user profile data. The profile pages are available only if user logged in. If not, the login page is shown. After login page, user will be redirected to the page selected previously.

Note: This is only an example that shows how you can use navigation handler decorator. Do NOT use it as a ready-to-use solution for login facility. Use Phase Listener instead.

Here is the snippet of the faces-config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN"
                              "http://java.sun.com/dtd/web-facesconfig_1_1.dtd">
<faces-config>
 <managed-bean>
  <managed-bean-name>LoginManager</managed-bean-name>
  <managed-bean-class>demo.LoginManagerBean</managed-bean-class>
  <managed-bean-scope>session</managed-bean-scope>
  <managed-property>
   <property-name>previousViewId</property-name>
   <value/>
  </managed-property>
  <managed-property>
   <property-name>previousAction</property-name>
   <value/>
  </managed-property>
  <managed-property>
   <property-name>previousMethod</property-name>
   <value/>
  </managed-property>
 </managed-bean>
....

 <application>
  <navigation-handler>org.exadel.LoginNavigationManager</navigation-handler>
  ...
 </application>
 
 ...
</faces-config>

The content of the Navigation Handler Decorator class:

/*
 * Created on 17.12.2004
 *
 * Author: Sergey Smirnov, Exadel., Inc.
 */

package org.exadel;

import javax.faces.application.NavigationHandler;
import javax.faces.context.FacesContext;

import demo.LoginManagerBean;



public class LoginNavigationManager extends NavigationHandler {
	
	public static final String RETURN_BACK = "!returnBack";
	public static final String LOGIN_GLOBAL = "globalLogin";

	NavigationHandler _base;
	String actionNameCurrent;
	String actionMethodCurrent;
	
	public LoginNavigationManager (NavigationHandler base) {
	 super();
	 _base = base;
	 
	}

	public void handleNavigation(FacesContext fc, String actionMethod, String actionName) {
		 actionNameCurrent = actionName;
		 actionMethodCurrent = actionMethod;
  		 LoginManagerBean loginManager = (LoginManagerBean)fc.getApplication()
		 	.createValueBinding("#{LoginManager}").getValue(fc);
	 
		 if (actionNameCurrent!=null) {
		 	if (actionNameCurrent.equals(RETURN_BACK)) {
		 		navigationCase4ReturningBack(fc, loginManager);
		 	}
		 } 	
	
		 navigationCase4Login(fc, loginManager);
	 
		 _base.handleNavigation(fc, actionMethodCurrent, actionNameCurrent);
	}

	
	private void navigationCase4Login(FacesContext fc, LoginManagerBean loginManager) {
	
		boolean loginStatus = loginManager.getLogin();
		
		if (! loginStatus && 
			actionNameCurrent!=null &&
			! actionNameCurrent.equals(LOGIN_GLOBAL)) {
			
			//store the current navigation position
			loginManager.setPreviousAction(actionNameCurrent);
			loginManager.setPreviousMethod(actionMethodCurrent);
			loginManager.setPreviousViewId(fc.getViewRoot().getViewId());
			loginManager.setReturnRequired(true);
			
			actionNameCurrent=LOGIN_GLOBAL;
		}
	}
	private void navigationCase4ReturningBack(FacesContext fc, LoginManagerBean loginManager) {
 		if (loginManager.getLogin()) {
			//restore the previous navigation position 			
 			actionNameCurrent = loginManager.getPreviousAction();
 			actionMethodCurrent = loginManager.getPreviousMethod();
 			fc.getViewRoot().setViewId(loginManager.getPreviousViewId());
 			loginManager.setReturnRequired(false);
 		}

	}
	
}

Credit: The example has been created with using Exadel JSF Studio Professional Edition, version 1.3. The JSF Studio (Eclipse) project with the source code is available at: http://www.exadel.com/downloads/jsf/examples/jsf-loginRedirect.zip(1.7M)

See Also:

JSF Navigation by Examples


Overview the standard navigation mechanism in JavaServer Faces

How to do Smart Navigation Handler in JavaserverTM Faces


Now itís really time to make smart navigation system