In this tutorial, we show you how to integrate JSF 2.0 with Spring.

Directory structure of this example

jsf2-spring-example-folder

1. Project Dependencies

Use Maven to grab all the JSF 2.0 and Spring library dependencies.

File : pom.xml


  4.0.0
  com.mkyong.common
  JavaServerFaces
  war
  1.0-SNAPSHOT
  JavaServerFaces Maven Webapp
  http://maven.apache.org

       java.net.m2
       java.net m2 repo
       http://download.java.net/maven/2

  	 

		org.springframework
		spring
		2.5.6

		org.springframework
		spring-web
		2.5.6

    <!-- For Java EE Application Server, uncomment this library 
  	and comment the rest of the libraries
    
      javax.faces
      jsf-api
      2.0
      provided
    
     -->

  	
  	

	  com.sun.faces
	  jsf-api
	  2.1.0-b03

	  com.sun.faces
	  jsf-impl
	  2.1.0-b03

	

	  org.glassfish.web
	  el-impl
	  2.2

        

	  javax.servlet
	  jstl
	  1.2

	  javax.servlet
	  servlet-api
	  2.5

	  javax.servlet.jsp
	  jsp-api
	  2.1

    JavaServerFaces

           org.apache.maven.plugins
           maven-compiler-plugin
           2.3.1

               1.6
               1.6

2. Spring Bean

A Spring bean declaration, later uses Spring to DI this “userBo” into JSF 2.0 managed bean property.

File : UserBo.java

package com.mkyong.user.bo;

public interface UserBo{

	public String getMessage();

}

File : UserBoImpl.java

package com.mkyong.user.bo.impl;

import com.mkyong.user.bo.UserBo;

public class UserBoImpl implements UserBo{

	String name;

	public void setName(String name) {
		this.name = name;
	}

	public String getMessage() {

		return "JSF 2 + Spring Integration saying : " + name;

	}

}

File : applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

	<bean id="userBo" class="com.mkyong.user.bo.impl.UserBoImpl">
		<property name="name" value="mkyong.com" />
	</bean>

</beans>

3. JSF 2.0

A simple JSF 2.0 web application.

File : UserBean.java

package com.mkyong;

import java.io.Serializable;

import com.mkyong.user.bo.UserBo;

//DI via JSF managed bean
public class UserBean implements Serializable{

	//DI via Spring
	UserBo userBo;

	public void setUserBo(UserBo userBo) {
		this.userBo = userBo;
	}

	public String printMsgFromSpring() {

		return userBo.getMessage();

	}

}

File : default.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!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:h="http://java.sun.com/jsf/html"
      >

    <h:body>

    	<h1>JSF 2.0 + Spring Example</h1>

 		#{user.printMsgFromSpring()}

    </h:body>

</html>

JSF 2.0 + Spring Integration

The main concern is how to inject Spring “UserBo” bean into JSF managed bean “UserBean“.

File : UserBean.java

//DI via JSF managed bean
public class UserBean implements Serializable{

	//DI via Spring
	UserBo userBo;

1. Spring Listeners

Add following two Spring’s listeners into web.xml.

  <listener>
	<listener-class>
		org.springframework.web.context.ContextLoaderListener
	</listener-class>
  </listener>
  <listener>
	<listener-class>
		org.springframework.web.context.request.RequestContextListener
	</listener-class>
  </listener>

See full example of web.xml.

File : 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">

  <display-name>JavaServerFaces</display-name>

  <!-- Add Support for Spring -->
  <listener>
	<listener-class>
		org.springframework.web.context.ContextLoaderListener
	</listener-class>
  </listener>
  <listener>
	<listener-class>
		org.springframework.web.context.request.RequestContextListener
	</listener-class>
  </listener>

  <!-- Change to "Production" when you are ready to deploy -->
  <context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
  </context-param>

  <!-- Welcome page -->
  <welcome-file-list>
    <welcome-file>faces/default.xhtml</welcome-file>
  </welcome-file-list>

  <!-- JSF mapping -->
  <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <!-- Map these files with JSF -->
  <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>*.jsf</url-pattern>
  </servlet-mapping>
  <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>

</web-app>

2. SpringBeanFacesELResolver

Add SpringBeanFacesELResolver as el-resolver in faces-config.xml, and declare JSF managed bean like normal. Now, whenever JSF sees a bean name, it try JSF rules first, then Spring rules next. In this case, when JSF see #{userBo}, it will match it with Spring’s “userBo” bean.

File : faces-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<faces-config
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
    version="2.0">

	<application>
    	        <el-resolver>
    		    org.springframework.web.jsf.el.SpringBeanFacesELResolver
    	        </el-resolver>
  	</application>

	<managed-bean>
		<managed-bean-name>user</managed-bean-name>
		<managed-bean-class>com.mkyong.UserBean</managed-bean-class>
		<managed-bean-scope>session</managed-bean-scope>
		<managed-property>
			<property-name>userBo</property-name>
			<value>#{userBo}</value>
		</managed-property>
	</managed-bean>

</faces-config>
Note
Alternatively, you can add DelegatingVariableResolver in variable-resolver, but API variable-resolver is deprecated after JSF 1.1. You should use el-resolverinstead.

<application>
	<variable-resolver>
		org.springframework.web.jsf.DelegatingVariableResolver
	</variable-resolver>
</application>

3. Demo

Done, run it and see output below : http://localhost:8080/JavaServerFaces/faces/

jsf2-spring-example-output

Download Source Code

Advertisements
Comments
  1. Andy Res says:

    Very comprehensive. Thanks.

    Like

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s