Spring JSF Integration With Examples

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.
  2. Add the DelegatingVariableResolver in faces-config.xml file as shown below. Here el-resolver is the delegating variable resolver.

    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;

    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
  2. Create the interface CarDao.java as
  3. Create the implementation class CarImpl.java as

    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
  2. Add the base package details in applicationContext.xml to scan for service classes.

    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.

JSF-Spring-Integration-Example-450x326

Below image shows the final project structure in Eclipse.

JSF-Spring-Example-Project

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

By admin

Leave a Reply