How to integrate JSF and Spring Framework


If you would like to integrate the JSF MVC framework with the Spring Framework, you can use the following approach;
There are also other configurations  available however this one fits very well while developing web applications using JSF and Spring.

In this example, I am using the basic .jars required by Spring Framework. These jars are;

spring-web.jar
spring-webmvc-2.5.jar
org.springframework.expression-3.1.0.RC1.jar
org.springframework.core-3.1.0.RC1.jar
org.springframework.context-3.1.0.RC1.jar
org.springframework.beans-3.1.0.RC1.jar
org.springframework.asm-3.1.0.RC1.jar

You also the following jars inside your project’s lib folder in order to use JSF framework;

jstl.jar
standard.jar
jsf-impl.jar
jsf-api.jar
commons-beanutils.jar
commons-collections.jar
commons-digester.jar
commons-logging.jar

Here you can see the project’s library structure;

SpringJSFApplication.class this class will be used as  the main application inside the program, it also has a reference for the FooService class  initialized by Spring Framework.

package com.tunatore.spring.application;

import com.tunatore.spring.service.FooService;

/**
*
* @author tunatore
*/
public class SpringJSFApplication {

private FooService fooService;

/**
* @return the fooService
*/
public FooService getFooService() {
return fooService;
}

/**
* @param fooService the fooService to set
*/
public void setFooService(FooService fooService) {
this.fooService = fooService;
}

}

 Foo.class is used a regular bean and it is solely Java POJO

package com.tunatore.spring.bean;

/**
*
* @author tunatore
*/
public class Foo {
private String name = "FooBean Name is here!";

public Foo(String name) {
this.name = name;
}

/**
* @return the name
*/
public String getName() {
return name;
}

/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}

}

 FooService.class is used to get a Foo Object (or from Database)

package com.tunatore.spring.service;

import com.tunatore.spring.bean.Foo;

/**
*
* @author tunatore
*/
public class FooService {
public Foo getFooBean() {
return new Foo("This is a new Foo [initiated by Spring Framework]");
}
}

The following file is required to configure Spring beans

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--
 Document : applicationContext.xml
 Created on : 28 November 2011, 13:47
 Author : tunatore
 Description:
 applicationContext.xml file is used by
 Spring for bean initializations
-->
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">

<bean id="springJSFApplication" class="com.tunatore.spring.application.SpringJSFApplication">
 <property name="fooService" ref="fooService"/>
</bean>

<bean id="fooService" class="com.tunatore.spring.service.FooService"/>

</beans>

web.xml file should include the location of the applicationContext.xml and two Spring specific listeners
org.springframework.web.context.ContextLoaderListener
org.springframework.web.context.request.RequestContextListener

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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-app_2_5.xsd">
 <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>
 <session-config>
 <session-timeout>
 30
 </session-timeout>
 </session-config>
 <welcome-file-list>
 <welcome-file>faces/index.jsp</welcome-file>
 </welcome-file-list>
 <context-param>
 <param-name>contextConfigLocation</param-name>
 <param-value>/WEB-INF/applicationContext*.xml</param-value>
 </context-param>
 <listener>
 <listener-class>
 org.springframework.web.context.ContextLoaderListener
 </listener-class>
 </listener>
 <listener>
 <listener-class>
 org.springframework.web.context.request.RequestContextListener
 </listener-class>
 </listener>
</web-app>

With the following faces-config.xml file
fooService Service object will be initialized through Spring Framework. You  also need to define 
org.springframework.web.jsf.DelegatingVariableResolver inside this file.

faces-config.xml

<?xml version='1.0' encoding='UTF-8'?>

<!-- =========== FULL CONFIGURATION FILE ================================== -->

<faces-config version="1.2"
 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_1_2.xsd">

<application>
 <variable-resolver>
 org.springframework.web.jsf.DelegatingVariableResolver
 </variable-resolver>
 </application>
 <managed-bean>
 <managed-bean-name>SpringJSFApplication</managed-bean-name>
 <managed-bean-class>
 com.tunatore.spring.application.SpringJSFApplication
 </managed-bean-class>
 <managed-bean-scope>session</managed-bean-scope>
 <managed-property>
 <property-name>fooService</property-name>
 <value>#{fooService}</value>
 </managed-property>
 </managed-bean>
</faces-config>

This index page will be call fooService methods 

index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<f:view>
 <html>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
 <title>Spring JSF Integration Example</title>
 </head>
 <body>
 <h1><h:outputText value="#{SpringJSFApplication.fooService.fooBean.name}"/></h1>
 </body>
 </html>
</f:view>

When you run the program, you will see the following screen on the browser;

You can download the program’s source by clicking here (This is a war file including sources)

Comments or questions are welcomed

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

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