Here’s a long article to show you how to integrate JSF 2.0(also jsf 1.X) , Spring and Hibernate together. At the end of the article, you will create a page which display a list of the existing customer from database .This article is using for tell you how to integrate jsf,spring ,hibernate .Maybe you can expand it by add a button to add a record into DB or do anything else you like to . But first , you can follow me . all right , Let's integrate it now .
P.S In this example, we are using Oracle database and deploy to Jboss web container.
1. Project Structure
Directory structure of this example:
2. Table Script
Create a customer table and insert 2 dummy records.
CREATE
TABLE
customer_lwc (
CUSTOMER_ID
INTEGER
NOT
NULL
,
NAME
VARCHAR
(
45
)
NOT
NULL
,
ADDRESS
VARCHAR
(
255
)
NOT
NULL
,
CREATED_DATE DATE
NOT
NULL
,
PRIMARY
KEY
(CUSTOMER_ID)
)
INSERT
INTO
CUSTOMER_LWC
(CUSTOMER_ID, NAME, ADDRESS, CREATED_DATE)
VALUES
(
1
,
'
Liang Wei Cheng
'
,
'
ChongQing University
'
, SYSDATE);
INSERT
INTO
CUSTOMER_LWC
(CUSTOMER_ID, NAME, ADDRESS, CREATED_DATE)
VALUES
(
2
,
'
Tester
'
,
'
Singtel
'
, SYSDATE
-
1
);
3. Hibernate Stuff
A model class and Hibernate mapping file for customer table.
File : Customer.java
package
com.singtel.customer.model;
import
java.util.Date;
public
class
Customer{
public
long
customerId;
public
String name;
public
String address;
public
Date createdDate;
public
long
getCustomerId() {
return
customerId;
}
public
void
setCustomerId(
long
customerId) {
this
.customerId =
customerId;
}
public
String getName() {
return
name;
}
public
void
setName(String name) {
this
.name =
name;
}
public
String getAddress() {
return
address;
}
public
void
setAddress(String address) {
this
.address =
address;
}
public
Date getCreatedDate() {
return
createdDate;
}
public
void
setCreatedDate(Date createdDate) {
this
.createdDate =
createdDate;
}
}
File : Customer.hbm.xml
<?
xml version="1.0"
?>
<!
DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
>
<
hibernate-mapping
>
<
class
name
="com.singtel.customer.model.Customer"
table
="customer_lwc"
catalog
="OPS$ESHOP"
>
<
id
name
="customerId"
type
="long"
>
<
column
name
="CUSTOMER_ID"
/>
<
generator
class
="identity"
/>
</
id
>
<
property
name
="name"
type
="string"
>
<
column
name
="NAME"
length
="45"
not-null
="true"
/>
</
property
>
<
property
name
="address"
type
="string"
>
<
column
name
="ADDRESS"
not-null
="true"
/>
</
property
>
<
property
name
="createdDate"
type
="timestamp"
>
<
column
name
="CREATED_DATE"
length
="19"
not-null
="true"
/>
</
property
>
</
class
>
</
hibernate-mapping
>
4. Spring Stuff
Spring’s BO and DAO classes for business logic and database interaction.
File : CustomerBo.java
package
com.singtel.customer.bo;
import
java.util.List;
import
com.singtel.customer.model.Customer;
public
interface
CustomerBo{
void
addCustomer(Customer customer);
List
<Customer>
findAllCustomer();
}
File : CustomerBoImpl.java
package
com.singtel.customer.bo.impl;
import
java.util.List;
import
com.singtel.customer.bo.CustomerBo;
import
com.singtel.customer.dao.CustomerDao;
import
com.singtel.customer.model.Customer;
public
class
CustomerBoImpl
implements
CustomerBo{
CustomerDao customerDao;
public
void
setCustomerDao(CustomerDao customerDao) {
this
.customerDao =
customerDao;
}
public
void
addCustomer(Customer customer){
customerDao.addCustomer(customer);
}
public
List<Customer>
findAllCustomer(){
return
customerDao.findAllCustomer();
}
}
File : CustomerDao.java
package
com.singtel.customer.dao;
import
java.util.List;
import
com.singtel.customer.model.Customer;
public
interface
CustomerDao{
void
addCustomer(Customer customer);
List
<Customer>
findAllCustomer();
}
File : CustomerDaoImpl.java
package
com.singtel.customer.dao.impl;
import
java.util.Date;
import
java.util.List;
import
com.singtel.customer.dao.CustomerDao;
import
com.singtel.customer.model.Customer;
import
org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public
class
CustomerDaoImpl
extends
HibernateDaoSupport
implements
CustomerDao{
public
void
addCustomer(Customer customer){
customer.setCreatedDate(
new
Date());
getHibernateTemplate().save(customer);
}
public
List<Customer>
findAllCustomer(){
return
getHibernateTemplate().find("from Customer"
);
}
}
File : CustomerBean.xml
<?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-2.5.xsd">
<bean id="customerBo"
class
="com.singtel.customer.bo.impl.CustomerBoImpl" >
<property name="customerDao" ref="customerDao" />
</bean>
<bean id="customerDao"
class
="com.singtel.customer.dao.impl.CustomerDaoImpl" >
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
</beans>
5. Spring + Database
Configure database detail in Spring.
File : db.properties
jdbc.driverClassName=oracle.jdbc.
OracleDriver
jdbc
.url=jdbc
:oracle:thin
:@127.0.0.1:1521
:XE
jdbc
.username=ops$
eshop
jdbc
.password=password
File : DataSource.xml
<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-2.5.xsd">
<
bean
class
="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:/config/database/db.properties</value>
</property>
</bean>
<bean id="dataSource"
class
="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
</beans>
6. Spring + Hibernate
Integrate Hibernate and Spring via
LocalSessionFactoryBean
.
File : HibernateSessionFactory.xml
<?
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-2.5.xsd"
>
<!--
Hibernate session factory
-->
<
bean
id
="sessionFactory"
class
="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
>
<
property
name
="dataSource"
>
<
ref
bean
="dataSource"
/>
</
property
>
<
property
name
="hibernateProperties"
>
<
props
>
<
prop
key
="hibernate.dialect"
>
org.hibernate.dialect.MySQLDialect
</
prop
>
<
prop
key
="hibernate.show_sql"
>
true
</
prop
>
</
props
>
</
property
>
<
property
name
="mappingResources"
>
<
list
>
<!--
<value>
classpath:/com/singtel/customer/hibernate/Customer.hbm.xml
</value>
-->
<
value
>
com/singtel/customer/hibernate/Customer.hbm.xml
</
value
>
</
list
>
</
property
>
</
bean
>
</
beans
>
7. JSF 2.0
JSF managed bean to call Spring’s BO to add or get customer’s records from database.
File : CustomerBean.java
package
com.singtel;
import
java.io.Serializable;
import
java.util.List;
import
com.singtel.customer.bo.CustomerBo;
import
com.singtel.customer.model.Customer;
public
class
CustomerBean
implements
Serializable{
//
DI via Spring
CustomerBo customerBo;
List
<Customer>
customerList;
public
String name ;
public
String address ;
public
String getName() {
return
name;
}
public
void
setName(String name) {
this
.name =
name;
}
public
String getAddress() {
return
address;
}
public
void
setAddress(String address) {
this
.address =
address;
}
public
void
setCustomerBo(CustomerBo customerBo) {
this
.customerBo =
customerBo;
}
//
get all customer data from database
public
List<Customer>
getCustomerList(){
return
customerBo.findAllCustomer();
}
//
add a new customer data into database
public
String addCustomer(){
Customer cust
=
new
Customer();
cust.setName(getName());
cust.setAddress(getAddress());
customerBo.addCustomer(cust);
clearForm();
return
""
;
}
//
clear form values
private
void
clearForm(){
setName(
""
);
setAddress(
""
);
}
}
A JSF page to display existing customer records via
h:dataTable
and a few text components to allow user to insert new customer record into database.
File : test.jsp
<%
@ taglib uri
=
"
http://java.sun.com/jsf/core
"
prefix
=
"
f
"
%>
<%
@ taglib uri
=
"
http://java.sun.com/jsf/html
"
prefix
=
"
h
"
%>
<%
@ page import
=
"
java.util.Calendar
"
%>
<%
@ page import
=
"
java.text.SimpleDateFormat
"
%>
<
link
href
="css/table-style.css"
rel
="stylesheet"
type
="text/css"
/>
<
html
>
<
f:view
>
<
head
>
<
title
>
JSF 2.0 + Spring + Hibernate Example
</
title
>
<
meta
http-equiv
="Content-Type"
content
="text/html; charset=UTF-8"
/>
</
head
>
<
body
>
<
h1
>
JSF + Spring + Hibernate
</
h1
>
<
h:form
>
<
h:dataTable
value
="#{customer.customerList}"
var
="c"
styleClass
="order-table"
headerClass
="order-table-header"
rowClasses
="order-table-odd-row,order-table-even-row"
>
<
h:column
>
<
f:facet
name
="header"
>
<
h:outputText
value
="Customer ID"
/>
</
f:facet
>
<
h:outputText
value
="#{c.customerId}"
></
h:outputText
>
</
h:column
>
<
h:column
>
<
f:facet
name
="header"
>
<
h:outputText
value
="Name"
/>
</
f:facet
>
<
h:outputText
value
="#{c.name}"
></
h:outputText
>
</
h:column
>
<
h:column
>
<
f:facet
name
="header"
>
<
h:outputText
value
="Address"
/>
</
f:facet
>
<
h:outputText
value
="#{c.address}"
></
h:outputText
>
</
h:column
>
<
h:column
>
<
f:facet
name
="header"
>
<
h:outputText
value
="Created Date"
/>
</
f:facet
>
<
h:outputText
value
="#{c.createdDate}"
></
h:outputText
>
</
h:column
>
</
h:dataTable
>
</
h:form
>
</
body
>
</
f:view
>
</
html
>
8. JSF 2.0 + Spring
Integrate JSF 2.0 with Spring, see detail explanation here – JSF 2.0 + Spring integration example
File : applicationContext.xml
<
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-2.5.xsd"
>
<!--
Database Configuration
-->
<
import
resource
="classpath:/config/spring/beans/DataSource.xml"
/>
<
import
resource
="classpath:/config/spring/beans/HibernateSessionFactory.xml"
/>
<!--
Beans Declaration
-->
<
import
resource
="classpath:/com/singtel/customer/spring/CustomerBean.xml"
/>
</
beans
>
File : faces-config.xml
<?xml version="1.0"?>
<faces-config version="2.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xi
="http://www.w3.org/2001/XInclude" 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-facesconfig_2_0.xsd">
<managed-bean>
<managed-bean-name>customer</managed-bean-name>
<managed-bean-
class
>com.singtel.CustomerBean</managed-bean-
class
>
<managed-bean-scope>session</managed-bean-scope>
<managed-property>
<property-name>customerBo</property-name>
<value>#{customerBo}</value>
</managed-property>
</managed-bean>
<application>
<el-resolver>
org.springframework.web.jsf.el.SpringBeanFacesELResolver
</el-resolver>
</application>
</faces-config>
File : web.xml
<?
xml version="1.0" encoding="UTF-8"
?>
<
web-app
id
="WebApp_ID"
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"
>
<
display-name
>
JHibernateS
</
display-name
>
<!--
Regester faces-config.xml in web.xml
-->
<
context-param
>
<
param-name
>
javax.faces.CONFIG_FILES
</
param-name
>
<
param-value
>
/WEB-INF/faces-config.xml
</
param-value
>
</
context-param
>
<
context-param
>
<
param-name
>
javax.faces.PROJECT_STAGE
</
param-name
>
<
param-value
>
Development
</
param-value
>
</
context-param
>
<!--
Notice :context-param -> listener -> filter -> servlet Add Support
for Spring
-->
<
context-param
>
<
param-name
>
javax.faces.STATE_SAVING_METHOD
</
param-name
>
<
param-value
>
server
</
param-value
>
</
context-param
>
<
listener
>
<
listener-class
>
org.springframework.web.context.ContextLoaderListener
</
listener-class
>
</
listener
>
<
listener
>
<
listener-class
>
org.springframework.web.context.request.RequestContextListener
</
listener-class
>
</
listener
>
<
listener
>
<
listener-class
>
com.sun.faces.config.ConfigureListener
</
listener-class
>
</
listener
>
<!--
Faces Servlet
-->
<
servlet
>
<
servlet-name
>
Faces Servlet
</
servlet-name
>
<
servlet-class
>
javax.faces.webapp.FacesServlet
</
servlet-class
>
<
load-on-startup
>
1
</
load-on-startup
>
</
servlet
>
<!--
Faces Servlet Mapping
-->
<
servlet-mapping
>
<
servlet-name
>
Faces Servlet
</
servlet-name
>
<
url-pattern
>
*.jsf
</
url-pattern
>
</
servlet-mapping
>
<
welcome-file-list
>
<
welcome-file
>
default.jsf
</
welcome-file
>
</
welcome-file-list
>
</
web-app
>
9. Demo
Run it,display like following :

