This quickstart will show you how to start using JSF-Spring in a web application. It assumes you've got an existing web application. The first steps describe how to install and configure Spring and JSF. If you've got one or both of them installed already, just skip the corresponding sections.

Installing Spring

Download Spring (2.0) from http://www.springframework.org/download and extract the following jars to WEB-INF/lib:

Then, create a basic application context


<?xml version="1.0" encoding="UTF-8"?/gt;

and define the listener so that Spring initializes itself on application startup:

WEB-INF/web.xml (partial)


Installing JSF

Download either Sun's Reference Implementation (v1.1.01) from http://java.sun.com/javaee/javaserverfaces/download.html or MyFaces (Core 1.1.4 Distribution) from http://myfaces.apache.org/download.html and copy the jars from the lib directory of the downloaded archive to WEB-INF/lib. When using Sun's Reference Implementation, you gotta make sure you've got jstl.jar in your path since the distribution doesn't provide it.

Then, create a basic faces configuration


<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE faces-config PUBLIC
	"-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN"

and define the faces servlet that will start up JSF and handle all requests:

WEB-INF/web.xml (partial)

	<servlet-name>Faces Servlet</servlet-name>
	<servlet-name>Faces Servlet</servlet-name>

Installing JSF-Spring

Download JSF-Spring (4.0) from http://sourceforge.net/project/showfiles.php?group_id=107519 and extract the following jars to WEB-INF/lib:

Then, define the listener that initializes JSF-Spring on application startup:

WEB-INF/web.xml (partial)


Note that this is not a replacement for Spring's ContextLoaderListener, you need both. Furthermore, the order in which you define the listeners is important: First Spring's, then JSF-Spring's ContextLoaderListener.

Testing the Setup - a simple application

To verify that JSF-Spring is set up properly, we'll create two simple beans: One service bean providing data and one ui bean formatting it for rendering:


package de.mindmatters.faces.quickstart.service;

import java.util.Date;

public interface TimeService {
	Date getNow();


package de.mindmatters.faces.quickstart.service;

import java.util.Date;

public class TimeServiceImpl implements TimeService {
	public Date getNow() {
		return new Date();

WEB-INF/applicationContext.xml (partial)

<bean id="timeService" class="de.mindmatters.faces.quickstart.service.TimeServiceImpl" />


package de.mindmatters.faces.quickstart.ui;

import java.text.SimpleDateFormat;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;

import de.mindmatters.faces.quickstart.service.TimeService;

public class UiBean implements InitializingBean {
	private TimeService timeService = null;

	public void afterPropertiesSet() throws Exception {
		Assert.notNull(getTimeService(), "timeService must be set");

	public String getShortDate() {
		return SimpleDateFormat.getDateInstance(SimpleDateFormat.SHORT).format(

	public TimeService getTimeService() {
		return timeService;

	public void setTimeService(TimeService timeService) {
		this.timeService = timeService;

WEB-INF/faces-config.xml (partial)


As you can see, uiBean is managed by JSF and references timeService which is managed by Spring. Furthermore, it can benefit from various Spring features, namely InitializingBean in this case.

Now, create a simple page rendering the current date:


<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
	  <title>jsf-spring quickstart</title>
    	<h:outputText value="#{uiBean.shortDate}"/>

Remember to view this page by accessing /index.jsf with your browser, when using / or /index.jsp, your request won't be handled by FacesServlet and thus will produce errors.

mindmatters GmbH & Co. KG java.net member logo SourceForge.net Logo