Exemple d'intégration JSF 2.0 + Spring + Hibernate
Voici un long article pour vous montrer comment intégrer ensembleJSF 2.0,Spring etHibernate. À la fin de l'article, vous allez créer une page qui affichera une liste des clients existants de la base de données et une fonction «ajouter un client» pour permettre à l'utilisateur d'ajouter un nouveau client dans la base de données.
P.S In this example, we are using MySQL database and deploy to Tomcat 6 web container.
1. Structure du projet
Structure de répertoire de cet exemple


2. Script de table
Créez une table client et insérez 2 enregistrements fictifs.
DROP TABLE IF EXISTS `exampledb`.`customer`; CREATE TABLE `exampledb`.`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 exampledb.customer(customer_id, name, address, created_date) values(1, 'example1', 'address1', now()); insert into exampledb.customer(customer_id, name, address, created_date) values(2, 'example2', 'address2', now());
3. Hibernate Stuff
Une classe de modèle et un fichier de mappage Hibernate pour la table client.
Fichier: Customer.java
package com.example.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
}
Fichier: Customer.hbm.xml
4. Trucs de printemps
Classes BO et DAO de Spring pour la logique métier et l'interaction avec la base de données.
Fichier: CustomerBo.java
package com.example.customer.bo;
import java.util.List;
import com.example.customer.model.Customer;
public interface CustomerBo{
void addCustomer(Customer customer);
List findAllCustomer();
}
Fichier: CustomerBoImpl.java
package com.example.customer.bo.impl;
import java.util.List;
import com.example.customer.bo.CustomerBo;
import com.example.customer.dao.CustomerDao;
import com.example.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 findAllCustomer(){
return customerDao.findAllCustomer();
}
}
Fichier: CustomerDao.java
package com.example.customer.dao;
import java.util.List;
import com.example.customer.model.Customer;
public interface CustomerDao{
void addCustomer(Customer customer);
List findAllCustomer();
}
Fichier: CustomerDaoImpl.java
package com.example.customer.dao.impl;
import java.util.Date;
import java.util.List;
import com.example.customer.dao.CustomerDao;
import com.example.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 findAllCustomer(){
return getHibernateTemplate().find("from Customer");
}
}
Fichier: CustomerBean.xml
5. Spring + Database
Configurez les détails de la base de données dans Spring.
Fichier: db.properties
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/exampledb jdbc.username=root jdbc.password=password
Fichier: DataSource.xml
WEB-INF/classes/config/database/db.properties
6. Spring + Hibernate
Intégrez Hibernate et Spring viaLocalSessionFactoryBean.
Fichier: HibernateSessionFactory.xml
7. JSF 2.0
JSF a géré le bean pour appeler Spring’s BO pour ajouter ou obtenir les enregistrements des clients dans la base de données.
Fichier: CustomerBean.java
package com.example;
import java.io.Serializable;
import java.util.List;
import com.example.customer.bo.CustomerBo;
import com.example.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 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("");
}
}
Une page JSF pour afficher les enregistrements client existants viah:dataTable et quelques composants de texte pour permettre à l'utilisateur d'insérer un nouvel enregistrement client dans la base de données.
Fichier: default.xhtml
JSF 2.0 + Spring + Hibernate Example
Customer ID
#{c.customerId}
Name
#{c.name}
Address
#{c.address}
Created Date
#{c.createdDate}
Add New Customer
Name :
Address :
8. JSF 2.0 + Spring
Intégrez JSF 2.0 avec Spring, voir l'explication détaillée ici -JSF 2.0 + Spring integration example
Fichier: applicationContext.xml
Fichier: faces-config.xml
org.springframework.web.jsf.el.SpringBeanFacesELResolver customer com.example.CustomerBean session customerBo #{customerBo}
Fichier: web.xml
JavaServerFaces org.springframework.web.context.ContextLoaderListener org.springframework.web.context.request.RequestContextListener javax.faces.PROJECT_STAGE Development faces/default.xhtml Faces Servlet javax.faces.webapp.FacesServlet 1 Faces Servlet /faces/* Faces Servlet *.jsf Faces Servlet *.faces Faces Servlet *.xhtml
9. Demo
Exécutez-le, remplissez les données client et cliquez sur le bouton «soumettre».


Télécharger le code source
Téléchargez-le -JSF-2-Spring-Hibernate-Integration-Example.zip (19 Ko)