Integrating JSF, Spring and Hibernate

In this section we will explain you the process of Integrating Spring with JSF (Java Server Faces) technology.

Integrating JSF, Spring and Hibernate

Integrating JSF, Spring and Hibernate

        

In this section we will explain you the process of Integrating Spring with JSF (Java Server Faces) technology. This section gives you a brief description about Spring container (a WebApplicationContext), which contains all the 'business beans' present in the application.

Configuring Spring context(WebApplicationContext)

What is WebApplicationContext?

The WebApplicationContext is an interface that extends the ApplicationContext interface in the Spring framework. This interface is used to provide the configuration for a web application. The WebApplicationContext is ready only while application is running, it can even be reloaded in runtime if the implementation supports this.

Configuring WebApplicationContext

First of all it is necessary to configure the WebApplicationContext as a ContextListener in the web.xml file of the web application. Following code shows the configuration:

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

In case you are using an older version of the Servlet API (<2.3), you have to use Spring's ContextLoaderServlet in order to configure WebApplicationContext. You can use the following code in the web.xml file:

<servlet>
  <servlet-name>context</servlet-name>
  <servlet-class>
   org.springframework.web.context.ContextLoaderServlet
  </servlet-class>
  <load-on-startup>1</load-on-startup>
</servlet>

The ContextLoaderListener uses the configuration defined in applicationContext-hibernate.xml file and creates the object of WebApplicationContext for the application. The path of applicationContext-hibernate.xml is passed as <context-param>..</context-param> property. Following code can be used for this purpose:

<context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>/WEB-INF/applicationContext-hibernate.xml</param-value>
</context-param>

When the application is started, ContextLoaderListener loads the configuration parameters from applicationContext-hibernate.xml file and creates an object of WebApplicationContext and stores it the ServletContext of the web application.

Now our application can use the WebApplicationContext to find the beans present in it.

Getting the reference of Application context:

ApplicationContext appContext =
WebApplicationContextUtils.getWebApplicationContext(servletContext);

Getting the bean from the ApplicationContext 

Object o =appContext.getBean(beanName);

Developing Service Finder

In our application we will use Service Finder class to find the beans managed by Spring framework. Here is the code of the Service finder utility(ServiceFinder.java):

package  net.roseindia.web.common;
 
 
import javax.faces.context.FacesContext;
import javax.faces.context.ExternalContext;

import javax.servlet.ServletContext;


 import org.springframework.context.ApplicationContext; 
  
 import org.springframework.web.context.support.WebApplicationContextUtils; 
  
 import java.util.Map; 
  
 import javax.servlet.ServletRequest; 
 import javax.servlet.http.HttpServletRequest; 
  
 public class ServiceFinder { 


  public static Object findBean(String beanName){
  FacesContext context= FacesContext.getCurrentInstance();

  ServletContext servletContext = 
  (ServletContext)context.getExternalContext().getContext();
  ApplicationContext appContext =
  WebApplicationContextUtils.getWebApplicationContext(servletContext);

  Object o =appContext.getBean(beanName);

  return o;

  }

 }  

The findBean() method of ServiceFinder class is used to get the reference of in the backing beans of JSF.