Frameworks| Hibernate| Struts| JSF| JavaFX| Ajax| Spring| DOJO| JDO| iBatis| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
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

                          

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. 

                          

» View all related tutorials
Related Tags: c hibernate web spring application io this app ie add nat to bi e il it section li in ca

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.