Spring JSF Integration

Filed Under: JSF

Welcome to Spring JSF integration tutorial. JSF is a component based framework with great focus on user interfaces. Whereas Spring framework core principle is Dependency Injection. So it makes sense to integrate JSF with Spring framework where JSF will be used for user interfaces and Spring framework will be used for backend server side business logic.

Spring JSF

Springs can be integrated with JSF through DelegatingVariableResolver. Let’s now see how to integrate Spring JSF frameworks with an example.

  1. Add the following spring dependencies in pom.xml along with the standard JSF dependencies. Spring core and web dependencies are a minimum requirement for MVC based web application.
    
    <dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-core</artifactId>
    	<version>4.1.4.RELEASE</version>
    </dependency>
    
    <dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-web</artifactId>
    	<version>4.1.4.RELEASE</version>
    </dependency>
    
    <dependency>
    	<groupId>javax.inject</groupId>
    	<artifactId>javax.inject</artifactId>
    	<version>1</version>
    </dependency>
    
  2. Add the DelegatingVariableResolver in faces-config.xml file as shown below. Here el-resolver is the delegating variable resolver.
    
    <application>
        <el-resolver>
            org.springframework.web.jsf.el.SpringBeanFacesELResolver
        </el-resolver>
    </application>
    

    The el-resolver mentioning SpringBeanFacesELResolver connects the JSF front end values to the Spring backend service layer.

  3. Make the following entry in web.xml to add listeners provided by spring framework as;
    
    <listener>
    	<listener-class>
    		org.springframework.web.context.ContextLoaderListener
    	</listener-class>
    </listener>
    <listener>
    	<listener-class>
    		org.springframework.web.context.request.RequestContextListener
    	</listener-class>
    </listener>
    

    The listener class ties the spring framework entry points to the servlet context.

Our configuration files are ready, let’s look at the java classes for our Spring JSF example project.

  1. Create the managed bean class CarBean.java as
    
    package com.journaldev.jsfspring;
    
    import java.util.List;
    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.SessionScoped;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    
    @Component
    @ManagedBean
    @SessionScoped
    public class CarBean {
    
    	@Autowired
    	CarDao carDao;
    
    	public void setCarDao(CarDao carDao) {
    		this.carDao = carDao;
    	}
    
    	public List<String> fetchCarDetails() {
    
    		return carDao.getCarDetails();
    	}
    
    }
    
  2. Create the interface CarDao.java as
    
    package com.journaldev.jsfspring;
    
    import java.util.List;
    
    public interface CarDao {
    
    	public List<String> getCarDetails();
    
    }
    
  3. Create the implementation class CarImpl.java as
    
    package com.journaldev.jsfspring;
    
    import java.util.ArrayList;
    import java.util.List;
    import org.springframework.stereotype.Service;
    
    @Service
    public class CarImpl implements CarDao {
    
    	@Override
    	public List<String> getCarDetails() {
    
    		List<String> cars = new ArrayList<String>();
    
    		cars.add(0, "Santro");
    		cars.add(1, "Zen");
    		cars.add(2, "Alto");
    		cars.add(3, "Qualis");
    		cars.add(4, "Innova");
    
    		for (String c : cars) {
    			System.out.println(c);
    		}
    
    		return cars;
    
    	}
    
    }
    

    Notice the use of Spring annotations for service and wiring is done by @Autowired annotation. We can do these using Spring Bean configuration file too, we will see that in later sections.

Let’s create the view page in JSF.

  1. Create the JSF page car.xhtml as
    
    <?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"
    	xmlns:ui="http://java.sun.com/jsf/facelets">
    <h:head>
    	<title>JSF Spring Integration</title>
    </h:head>
    <h:body>
    
    	<h2>Car Names List</h2>
    
    	<ul>
    
    		<ui:repeat var="cars" value="#{carBean.fetchCarDetails()}">
    			<li><h3>#{cars}</h3></li>
    		</ui:repeat>
    
    	</ul>
    </h:body>
    </html>
    
  2. Add the base package details in applicationContext.xml to scan for service classes.
    
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    	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.1.xsd
    	http://www.springframework.org/schema/context
    	http://www.springframework.org/schema/context/spring-context-3.1.xsd">
    
    	<context:component-scan base-package="com.journaldev.jsfspring" />
    
    	<!-- If you prefer XML based Dependency Injection
    		remove annotation from class and uncomment below configuration -->	
    	<!-- 
    	<bean class="com.journaldev.jsfspring.CarImpl" id="carDAO" />
    	
    	<bean class="com.journaldev.jsfspring.CarBean" id="carBean">
    		<property name="carDao" ref="carDAO"></property>
    	</bean>
    	 -->
    	 
    </beans>
    

    Notice the commented code above, if you prefer XML based configuration then you can remove Spring annotations from java classes and uncomment these. You will get the same results.

Spring JSF Example Test

Our application is ready, just deploy it in your favorite servlet container and run it. You should get below output response.

Spring JSF

Below image shows the final project structure in Eclipse.
Spring JSF Integration

You can download the project from below link and play around with it to learn more.

Comments

  1. Nagarjun says:

    How to import this project in ti eclipse

  2. wklaczynski says:

    I suggest the use of “State Flow Faces”, solves problems with navigation in jsf.

    https://github.com/wklaczynski/state-flow-faces

  3. jesus macias says:

    Hi , Excuse me but , Do you have an example to spring jdbc and JSF?

  4. Alejandro says:

    What about using Hibernate in all this? Is there an special configuration to be done? Persistence Unit? I have been looking for this for a long time!!

  5. srihari konakanchi says:

    Thanks for compact required configuration and for sharing Pankaj!

  6. deepak says:

    The best tutorial i have seen so far. Crisp and clear 🙂 Thanks for sharing

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages