JSF 2.0 + Spring + Hibernate Integration Example
Here’s a long article to show you how to integrate JSF 2.0, 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 and a “add customer” function to allow user to add a new customer into database.
P.S In this example, we are using MySQL database and deploy to Tomcat 6 web container.
1. Project Structure
Directory structure of this example
2. Table Script
Create a customer table and insert 2 dummy records.
DROP TABLE IF EXISTS `mkyongdb`.`customer`; CREATE TABLE `mkyongdb`.`customer` ( `CUSTOMER_ID` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, `NAME` VARCHAR(45) NOT NULL, `ADDRESS` VARCHAR(255) NOT NULL, `CREATED_DATE` datetime NOT NULL, PRIMARY KEY (`CUSTOMER_ID`) ) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8; INSERT INTO mkyongdb.customer(customer_id, name, address, created_date) VALUES(1, 'mkyong1', 'address1', now()); INSERT INTO mkyongdb.customer(customer_id, name, address, created_date) VALUES(2, 'mkyong2', 'address2', now());
3. Hibernate Stuff
A model class and Hibernate mapping file for customer table.
File : Customer.java
package com.mkyong.customer.model; import java.util.Date; public class Customer{ public long customerId; public String name; public String address; public Date createdDate; //getter and setter methods }
File : Customer.hbm.xml
version="1.0"?>
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
name="com.mkyong.customer.model.Customer"
table="customer" catalog="mkyongdb">
name="customerId" type="long">
name="CUSTOMER_ID" />
class="identity" />
4. Spring Stuff
Spring’s BO and DAO classes for business logic and database interaction.
File : CustomerBo.java
package com.mkyong.customer.bo; import java.util.List; import com.mkyong.customer.model.Customer; public interface CustomerBo{ void addCustomer(Customer customer); List<Customer> findAllCustomer(); }
File : CustomerBoImpl.java
package com.mkyong.customer.bo.impl; import java.util.List; import com.mkyong.customer.bo.CustomerBo; import com.mkyong.customer.dao.CustomerDao; import com.mkyong.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.mkyong.customer.dao; import java.util.List; import com.mkyong.customer.model.Customer; public interface CustomerDao{ void addCustomer(Customer customer); List<Customer> findAllCustomer(); }
File : CustomerDaoImpl.java
package com.mkyong.customer.dao.impl; import java.util.Date; import java.util.List; import com.mkyong.customer.dao.CustomerDao; import com.mkyong.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
version="1.0" encoding="UTF-8"?>
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">
id="customerBo"
class="com.mkyong.customer.bo.impl.CustomerBoImpl" >
name="customerDao" ref="customerDao" />
5. Spring + Database
Configure database detail in Spring.
File : db.properties
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/mkyongdb jdbc.username=root jdbc.password=password
File : DataSource.xml
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">
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
name="location">
WEB-INF/classes/config/database/db.properties
6. Spring + Hibernate
Integrate Hibernate and Spring via
LocalSessionFactoryBean
.
File : HibernateSessionFactory.xml
version="1.0" encoding="UTF-8"?>
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">
id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
name="dataSource">
bean="dataSource"/>
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.mkyong; import java.io.Serializable; import java.util.List; import com.mkyong.customer.bo.CustomerBo; import com.mkyong.customer.model.Customer; public class CustomerBean implements Serializable{ //DI via Spring CustomerBo customerBo; public String name; public String address; //getter and setter methods 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 : default.xhtml
version="1.0" encoding="UTF-8"?>
8. JSF 2.0 + Spring
Integrate JSF 2.0 with Spring, see detail explanation here – JSF 2.0 + Spring integration example
File : applicationContext.xml
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">
resource="classes/config/spring/beans/DataSource.xml"/>
resource="classes/config/spring/beans/HibernateSessionFactory.xml"/>
resource="classes/com/mkyong/customer/spring/CustomerBean.xml"/>
File : faces-config.xml
version="1.0" encoding="UTF-8"?>
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-facesconfig_2_0.xsd"
version="2.0">
org.springframework.web.jsf.el.SpringBeanFacesELResolver
File : web.xml
version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
JavaServerFaces
9. Demo
Run it, fill in the customer data and click on the “submit” button.
Download Source Code
Download It – JSF-2-Spring-Hibernate-Integration-Example.zip (19KB)
No comments:
Post a Comment