Latest Entries »

Saturday, July 11, 2009

Integrating JSF, Hibernate and Spring

Hello Folks,This post describes the steps to configure Spring Hibernate JSF application. Integration of these three technologies makes a solid framework for web application development.
Creating the application with out IDE:Download the jars for JSF (Ex: Icefaces).Download the jars for Hibernate library.
Download the jars for Spring Library.
Jar's need are
ant-1.6.5.jar, ant-antlr-1.6.5.jar, ant-junit-1.6.5.jar, ant-launcher-1.6.5.jar, ant-swing-1.6.5.jar, antlr-2.7.6.jar, asm-attrs.jar, asm.jar, c3po-0.9.1.jar, cglib-2.1.3.jar, checkstyle-all.jar, cleanimports.jar, commons-beanutils-1.7.0.jar, commons-codec-1.3.jar, commons-collections-2.1.1.jar, commons-collections-3.1.1.jar, commons.digester-1.6.jar, commons-el-1.0.jar, commons-fileupload-1.0.jar, commons-lang-2.1.jar, commons-logging-1.0.4.jar, commons-validator-1.3.1.jar, concurrent-1.3.2.jar, connector.jar, dom4j-1.6.1.jar, ehcache-1.2.3.jar, hibernate3.jar, jass.jar, jacc-1_0-fr.jar, javassist.jar, jaxen-1.1-beta-7.jar, jdbc2_0-stdext.jar, jgroups-2.2.8.jar, jstl-1.1.0.jar, jta.jar, junit-3.8.1.jar, log4j-1.2.11.jar, myfaces-api-1.1.5.jar, myfaces-impl-1.1.5.jar, mysql-connector-java-3.0.16-ga-bin.jar, mysqlconnector-java-3.1.6-bin.jar, oro-2.0.8.jar, oscache-2.1.jar, proxool-0.8.3.jar, spring.jar, swarmcache-1.0rc2.jar, syndiag2.jar, tomahawk-1.1.6.jar, versioncheck.jar, xerces-2.6.2.jar, xml-apis.jar



















Now create the web application and copy all the above lib files under the WEB-INF folder.

Creating the web project with the MyEclipse IDE:Create New Web Application, add JSF capabilities (you can add different kind of flavours of JSF to your application Ex: Icefaces, Richfaces). Now add the spring capabilities and hibernate capabilities.
Create hibernate POJO's and DAO's classes (Hibernate Annotaions library is required to build POJO's & DAO's). Register all the POJO & DAO classes in the spring application contect file, to create hibernate POJO & DAO classes select the MyEclipse Hibernate Prespective. Add the database driver and connect to the database using DB browser. Navigate to the database-->tables, select all the tables and right click go to hibernate reverse engineering option. MyEclipse will take you to the several screens, here you need to select create POJO and create DAO options which will generate POJO & DAO classes.Register all the POJO & DAO classes in the applicationContext.xml file
Sample Code :












Add this spring context file in web.xml




At this point our web application got the relationship between the spring and hibernate and we registered them in web.xml. Now the tedious part of the application is to integrate this part with the JSF.

To integrate JSF and Spring , Spring API provides a variable resolver called DelegatingVariableResolver (but dude it work's only with JSF1.1 and it is not compatible with JSF1.2)




If we create one custom funtion works as DelegatingVariableResolver, then our application will be independent of spring API.


Sample Code:
import javax.faces.context.Facescontext;
import javax.servlet.ServletContext;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

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;
}
}










And call this funtion in your managed bean like :

ProductionDAO productionDAO = (ProductionDAO) ServiceFinder
.findBean("ProductionDAO");

This completes the integration part of JSF, HIbernate and Spring.

Application Architechture:

Web application consists of 3 layers
1. Presentaion Layer
2. Business Layer
3. Data Access Layer


In our application the presentation layer, business layer and data access layer are physically located on the same JEE server. The different layers of the application are isolated from each other and connected through well defined interfaces.








Presentation Layer

JSF is used to build the presentation layer of the application. JFS allows us to create rich gui for web application. It resolves the technical challenges of creating rich gui web application. In this layer we have jsp pages and JSF components. All the request to the web server are passes through faces servlet.

Business Layer

The POJO classes and classes to process the business logic are used to create the Business Layer. The POJO classes with the help of spring framework creates an ideal solution to implement the Business Layer.

Data Access Layer

The data access layer handles all the logic to save and retrieve the data from data base. Hibernate O/R mapping tools is an ideal solution for enterprise application of any size. Hibernate handles all the logic to store and retrieve POJO objects. It also handles resource management and transaction management activities.

0 comments: