JSF-Spring

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

WEB-INF/applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?/gt;
<!DOCTYPE beans PUBLIC
	"-//SPRING//DTD BEAN//EN"
	"http://www.springframework.org/dtd/spring-beans.dtd">
<beans></beans>

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

WEB-INF/web.xml (partial)

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

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

WEB-INF/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></faces-config>

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

WEB-INF/web.xml (partial)

<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>*.jsf</url-pattern>
</servlet-mapping>

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)

<listener>
	<listener-class>de.mindmatters.faces.spring.context.ContextLoaderListener</listener-class>
</listener>

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:

de/mindmatters/faces/quickstart/service/TimeService.java

package de.mindmatters.faces.quickstart.service;

import java.util.Date;

public interface TimeService {
	Date getNow();
}

de/mindmatters/faces/quickstart/service/TimeServiceImpl.java

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" />

de/mindmatters/faces/quickstart/ui/UiBean.java

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(
				getTimeService().getNow());
	}

	public TimeService getTimeService() {
		return timeService;
	}

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

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

<managed-bean>
	<managed-bean-name>uiBean</managed-bean-name>
	<managed-bean-class>de.mindmatters.faces.quickstart.ui.UiBean</managed-bean-class>
	<managed-bean-scope>request</managed-bean-scope>
	<managed-property>
		<property-name>timeService</property-name>
		<value>#{timeService}</value>
	</managed-property>
</managed-bean>

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:

index.jsp

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

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