Spring and JSF Integrated

This page describes the process of integrating the Spring framework with JSF. We will take an application that already has JSF and Spring components fully initialized and ready to go.

On this page we will cover 2 aspects of Spring JSF integration.

  1. Getting a JSF managed Bean to deligate to a Spring Managed Bean.
  2. Use a pure Spring Managed Bean in place of the JSF bean.


  • Maven2
  • Basic understanding of IOC and The Spring
  • Basic understanding of JSF

Before we can start we need to create the sample project. We are going to use a maven archetype.

Creating the Project

We will create this project using an archetype and walk thru the different parts that make this all work.


mvn archetype:generate -DarchetypeGroupId=org.vtechfw -DarchetypeArtifactId=springmvc-jsf-archetype -DarchetypeVersion=1.0-SNAPSHOT -DarchetypeRepository=http://www.vermatech.com/m2repo

Ignore the warning about checksum file for now. (trying to figure out how to fix that. If you know then please comment)

Maven will prompt you for some information. It is best to answer like the following.

groupId: testpackage
artifactId: springJSF

Hit enter for defaults for the rest of the questions.


cd to the project’s directory. (cd springJSF)

mvn jetty:run

View the project

Navigate to the following page: http://localhost:8080/

  1. The spring mvc configuration is read applicationContext.xml and spring-servlet.xml
  2. The faces configuration file is read faces-config.xml

The system completes the initialization and the web application is ready to serve requests at http://localhost:8080

The application displays a welcome page with 2 links. One points to the Faces JSP file that displays a text box that allows the user to enter values and submit. The second link displays a welcome page that is rendered by the SpringMVC framework.

1. Having JSF Managed Beans Delegate to Spring Managed Beans

You would typically want to do this if you already have jsf managed beans defined in the faces-config.xml file.

If you are starting fresh and purely want to use Spring beans as JSF managed beans then skip to section 2.

The DelegatingVariableResolver class provides the ability for Spring managed beans to be injected into JSF managed beans. This is a powerful technique to allow business logic that is managed in the spring container available for JSF applications to use.

How to get JSF to use Spring Beans

Enable the JSF system to use DelegatingVariableResolver.

Add the following in the faces-config.xml


Create a Spring Managed Bean

Actually you dont need to create one if you already generated the project. For documentation purposes the bean is listed here.

package testpackage.quickstart.springmvc;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.stereotype.Service;

public class DatePrintModelImpl implements DatePrintModel {
        private DateFormat dateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");

        public String getDateAsString() {
                System.out.println("getDataAsString called.");
                return dateFormat.format(new Date());

Setup the JSF Managed Bean

Add the DatePrintModel and its setters and getters to the managed been below.

After the change it should look something like this.

package testpackage.jsftest;

import javax.faces.event.ValueChangeEvent;

import testpackage.quickstart.springmvc.DatePrintModel;

public class MessageModel {

        private DatePrintModel datePrintModel;

        public DatePrintModel getDatePrintModel() {
                return datePrintModel;

        public void setDatePrintModel(DatePrintModel datePrintModel) {
                this.datePrintModel = datePrintModel;

        public void printMessage(ValueChangeEvent e) {
                System.out.println("old value was: " + e.getOldValue());
                System.out.println("new value is: " + e.getNewValue());

        public void callSpringModel() {
                System.out.println("calling the spring model...");

Modify the faces-config.xml to look like this…

<?xml version='1.0' encoding='UTF-8'?>
<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
              xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd"


As you can see above the managed-bean property has been added.

Modify the JSP


<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core" %>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html" %>

        <title>Hello World JSF Example</title>
                <p>This is a simple hello world page using Faces.</p>
                <p>Enter your message here: <br/>
                <h:inputText valueChangeListener="#{messageModel.printMessage}" value="#{message}" size="35"/></p>
                <h:commandButton value="Submit" action="#{messageModel.callSpringModel}"/>
    <p>after submit check server console for output.</p>

Run and view The Project

mvn jetty:run


2. Use a pure Spring Managed Bean in place of the JSF bean.

Getting JSF to Purely use Spring Bean

As you have seen above you can take an existing managed bean and inject spring managed beans. This next section will show you how to use a spring managed bean as a JSF backing bean.

Doing it this way simplifies the faces-config.xml.

Create the following file in

package com.test.quickstart.springmvc;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

public class SpringManagedBean {
        private String message;
        public String getMessage() {
                return message;
        public void setMessage(String message) {
                this.message = message;
        public void testMethod() {
                System.out.println("test method ran with message: " + message);

Insert the following into your spring configuration…

<context:component-scan base-package="com.test.quickstart.springmvc"/>

Create the following jsp:


<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core" %>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html" %>

        <title>Spring Managed Bean Example</title>
                <p>This is a simple hello world page using Faces and Spring Managed Bean.</p>
                <p>Enter your message here: <br/>
                <h:inputText value="#{springManagedBean.message}" size="35"/></p>
                <h:commandButton value="Submit" action="#{springManagedBean.testMethod}"/>
    <p>after submit check server console for output.</p>

Run and view The Project

mvn jetty:run

Navigate to:

This site is a collaborative effort! The complete text and sourcecode for this is available on GitHub. Corrections and enhancements are welcome, please make the change and submit a pull request in the comment area below.

0 Responses to “Spring and JSF Integrated”

  1. Leave a Comment

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

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 77 other followers

November 2009
« Oct   Dec »

Blog Stats

  • 847,163 hits

%d bloggers like this: