I will show how to integrate Spring 3, JSF 2 and Hibernate 3 in the following example. You may also read JSF 2, Spring 4, Hibernate 4 and Maven Integration
Download the required jars for Spring from https://spring.io, JSF from https://javaserverfaces.java.net/download.html and Hibernate from http://hibernate.org/orm/downloads/
Create web application using NetBeans or Eclipse or any Java based IDE.
Create Table in MySQL
CREATE TABLE cds ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, title varchar(200) COLLATE latin1_general_ci DEFAULT NULL, interpret varchar(200) COLLATE latin1_general_ci DEFAULT NULL, PRIMARY KEY (id) ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
insert into cds(id,title,interpret) values (1,'Beauty','Ryuichi Sakamoto'), (4,'Goodbye Country (Hello Nightclub)','Groove Armada'), (5,'Glee','Bran Van 3000');
Create Hibernate configurations and Model class
Configuration file : hibernate.cfg.xml in classpath
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/cdcol?zeroDateTimeBehavior=convertToNull</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.current_session_context_class">thread</property> <property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property> <property name="hibernate.show_sql">true</property> <property name="hibernate.format_sql">true</property> <property name="hibernate.jdbc.batch_size">50</property> <property name="hibernate.connection.autocommit">true</property> <mapping resource="com/jsfspring/domain/Cds.hbm.xml"/> </session-factory> </hibernate-configuration>
Hibernate mapping file : Cds.hbm.xml in com.jsfspring.spring.domain package
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <!-- Generated Nov 20, 2013 12:51:46 PM by Hibernate Tools 3.6.0 --> <hibernate-mapping> <class name="com.jsfspring.domain.Cds" table="cds" catalog="cdcol"> <id name="id" type="java.lang.Long"> <column name="id" /> <generator class="identity" /> </id> <property name="title" type="string"> <column name="title" length="200" /> </property> <property name="interpret" type="string"> <column name="interpret" length="200" /> </property> </class> </hibernate-mapping>
Model Class : Cds.java
package com.jsfspring.domain; public class Cds implements java.io.Serializable { private Long id; private String title; private String interpret; public Cds() { } public Cds(String title, String interpret) { this.title = title; this.interpret = interpret; } public Long getId() { return this.id; } public void setId(Long id) { this.id = id; } public String getTitle() { return this.title; } public void setTitle(String title) { this.title = title; } public String getInterpret() { return this.interpret; } public void setInterpret(String interpret) { this.interpret = interpret; } }
Services
package com.jsfspring.spring.service; import com.jsfspring.domain.Cds; import java.util.List; public interface CDService { List<Cds> getAllCDs(); void addNewCD(Cds cd); void deleteCD(Cds cd); }
package com.jsfspring.spring.service.impl; import com.jsfspring.domain.Cds; import com.jsfspring.spring.dao.CDDao; import com.jsfspring.spring.service.CDService; import java.util.List; import org.springframework.transaction.annotation.Transactional; public class CDServiceImpl implements CDService { CDDao cdDao; public CDDao getCdDao() { return cdDao; } public void setCdDao(CDDao cdDao) { this.cdDao = cdDao; } @Override @Transactional public List<Cds> getAllCDs() { return cdDao.getAllCDs(); } @Override @Transactional public void addNewCD(Cds cd) { cdDao.addNewCD(cd); } @Override @Transactional public void deleteCD(Cds cd) { cdDao.deleteCD(cd); } }
Spring DAO layer
package com.jsfspring.spring.dao; import com.jsfspring.domain.Cds; import java.util.List; public interface CDDao { List<Cds> getAllCDs(); void addNewCD(Cds cd); void deleteCD(Cds cd); }
package com.jsfspring.spring.dao.impl; import com.jsfspring.domain.Cds; import com.jsfspring.spring.dao.CDDao; import java.util.List; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; public class CDDaoImpl extends HibernateDaoSupport implements CDDao { @Override public List<Cds> getAllCDs() { return getHibernateTemplate().find("from Cds"); } @Override public void addNewCD(Cds cd) { getHibernateTemplate().save(cd); } @Override public void deleteCD(Cds cd) { getHibernateTemplate().delete(cd); } }
Spring bean for CD – cd.xml in com.jsfspring.spring.resources package
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd "> <bean id="cdService" class="com.jsfspring.spring.service.impl.CDServiceImpl"> <property name="cdDao" ref="cdDao"/> </bean> <bean id="cdDao" class="com.jsfspring.spring.dao.impl.CDDaoImpl"> <property name="sessionFactory" ref="sessionFactory"/> </bean> </beans>
descriptor file – web.xml in WEB-INF
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <context-param> <param-name>javax.faces.PROJECT_STAGE</param-name> <param-value>Development</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>Faces Servlet</servlet-name> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>*.jsf</url-pattern> </servlet-mapping> <session-config> <session-timeout> 30 </session-timeout> </session-config> <welcome-file-list> <welcome-file>index.jsf</welcome-file> </welcome-file-list> </web-app>
Java Server Faces Configuration file – faces-config.xml in WEB-INF
<?xml version='1.0' encoding='UTF-8'?> <faces-config version="2.2" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"> <!-- Allows you to inject Spring beans into JSF managed beans... --> <application> <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver> </application> </faces-config>
Spring configuration file – applicationContext.xml in WEB-INF
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"> <tx:annotation-driven transaction-manager="transactionManager" /> <!--Hibernate session factory configuration--> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> </bean> <!--Hibernate transaction manager--> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <!--bean resources--> <import resource="classes/com/jsfspring/spring/resources/cd.xml"/> </beans>
Create JSF ManagedBean
package com.jsfspring.jsf.mbean; import com.jsfspring.domain.Cds; import com.jsfspring.spring.service.CDService; import java.io.Serializable; import java.util.List; import javax.faces.application.FacesMessage; import javax.faces.bean.ManagedBean; import javax.faces.bean.ManagedProperty; import javax.faces.bean.ViewScoped; import javax.faces.context.FacesContext; @ManagedBean @ViewScoped public class CDMBean implements Serializable { /** * * Spring cd service is injected */ @ManagedProperty(value = "#{cdService}") private CDService cdService; private String cdTitle; private String cdInterpret; private List<Cds> cds; /** * Creates a new instance of CDMBean */ public CDMBean() { } public CDService getCdService() { return cdService; } public void setCdService(CDService cdService) { this.cdService = cdService; } public String getCdTitle() { return cdTitle; } public void setCdTitle(String cdTitle) { this.cdTitle = cdTitle; } public String getCdInterpret() { return cdInterpret; } public void setCdInterpret(String cdInterpret) { this.cdInterpret = cdInterpret; } public List<Cds> getCds() { if (cds == null) { cds(); } return cds; } public void setCds(List<Cds> cds) { this.cds = cds; } private void cds() { cds = cdService.getAllCDs(); } public String addNewCd() { Cds newCD = new Cds(); newCD.setTitle(cdTitle); newCD.setInterpret(cdInterpret); cdService.addNewCD(newCD); setCdTitle(""); setCdInterpret(""); FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("CD Successfully added")); return null; } public String deleteCD(Cds cd) { cdService.deleteCD(cd); cds(); FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("CD Successfully deleted")); return null; } }
Thanks for your reading. Please leave a comment.
Hello Soumitra,
i need your help, i don’t understand the role of Spring bean for CD in your tutorial and i need the javaBean with the index.xhtml, i work with netbeans 8.0.1
Thank you very much
I have just created it to separate beans from other spring config. you can put the beans in applicationContext.xml file also instead of creating separate c0onfig.