PrimeFaces + Spring + Hibernate 整合例子

2018-03-01 11:15:46来源:oschina作者:巨轮人点击

分享

我们将PrimeFaces作为UI框架,Spring用在应用的业务层,Hibernate用作数据连接层。最后通过一个CRUD的应用运行说明整合成功。


0.准备要素
Maven3
MySQL5.5
Tomcat7
Eclipse(可选)
1.创建数据库和表

在MySQL中创建数据库primefacesspring,并执行createTable.sql


CREATE TABLE `customer` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(45) NOT NULL,
`surname` varchar(45) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=19 DEFAULT CHARSET=utf8;
2.利用Maven创建一个WAR工程

pom.xml如下


<?xml version="1.0" encoding="UTF-8"?>

4.0.0
com.lun
primefacesSpring
1.0.0-SNAPSHOT
war

4.1.0.Final
3.2.5.RELEASE
false
1.8
1.8
UTF-8



org.primefaces
primefaces
6.1



com.sun.faces
jsf-api
2.1.11


com.sun.faces
jsf-impl
2.1.11


javax.servlet
jstl
1.2


javax.servlet
javax.servlet-api
3.1.0
provided


javax.servlet.jsp
jsp-api
2.1
provided



org.glassfish.web
el-impl
2.2



org.springframework
spring-core
${spring.version}


org.springframework
spring-context
${spring.version}


org.springframework
spring-web
${spring.version}


org.springframework
spring-aop
${spring.version}


org.springframework
spring-context-support
${spring.version}


org.springframework
spring-aspects
${spring.version}


org.springframework
spring-tx
${spring.version}


org.springframework
spring-orm
${spring.version}


org.springframework
spring-test
${spring.version}



org.hibernate
hibernate-core
${hibernate.version}




com.mchange
c3p0
0.9.5.2


mysql
mysql-connector-java
6.0.6




primefacesSpring


org.apache.maven.plugins
maven-compiler-plugin
2.3.2

1.6
1.6




3.创建WEB-INF/web.xml,配置加载Spring和JSF环境的监听器。
<?xml version="1.0" encoding="UTF-8"?>
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">
PrimeFaces Web Application

contextConfigLocation

/WEB-INF/applicationContext.xml




org.springframework.web.context.ContextLoaderListener




org.springframework.web.context.request.RequestContextListener




javax.faces.PROJECT_STAGE
Development



hello.xhtml



Faces Servlet
javax.faces.webapp.FacesServlet
1



Faces Servlet
/faces/*


Faces Servlet
*.jsf


Faces Servlet
*.faces


Faces Servlet
*.xhtml

4.创建Spring配置文件WEB-INF/applicationContext.xml

配置数据源,Hibernate,事务的信息配置。


<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">



destroy-method="close">













com.lun.model.Customer




org.hibernate.dialect.MySQL5Dialect
true










5.创建WEB-INF/faces-config.xml

该配置文件主要用处主要整合Spring和Primefaces的托管Beans


<?xml version="1.0" encoding="utf-8"?>
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

6.创建一个领域实体和其数据连接层

领域模型:


[@Entity](https://my.oschina.net/u/1260961)
@Table(name="CUSTOMER")
public class Customer implements Serializable{ private static final long serialVersionUID = 6805770934683941123L;
[@Id](https://my.oschina.net/u/3451001)
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="ID", unique = true, nullable = false)
private Integer id;

@Column(name="NAME", unique = true, nullable = false)
private String name;

@Column(name="SURNAME", unique = true, nullable = false)
private String surname; //Omit setters and getters
@Override
public String toString() {
StringBuffer strBuff = new StringBuffer();
strBuff.append("id : ").append(getId());
strBuff.append(", name : ").append(getName());
strBuff.append(", surname : ").append(getSurname());
return strBuff.toString();
}
}

数据连接层:


@Repository
public class CustomerDAO {

@Autowired
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public void addCustomer(Customer customer) {
getSessionFactory().getCurrentSession().save(customer);
}
public void deleteCustomer(Customer customer) {
getSessionFactory().getCurrentSession().delete(customer);
}
public void updateCustomer(Customer customer) {
getSessionFactory().getCurrentSession().update(customer);
}
@SuppressWarnings("rawtypes")
public Customer getCustomerById(int id) {
List list = getSessionFactory().getCurrentSession()
.createQuery("from Customerwhere id=?").setParameter(0, id).list();
return (Customer) list.get(0);
}
@SuppressWarnings("unchecked")
public List getCustomers() {
return getSessionFactory().getCurrentSession().createQuery("from Customer")
.list();
}
}
7.创建main.xhtml视图文件
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">


The CRUD Example






var="i"
value="#{customerMB.customerList}"
style="width: 50%">

#{i.id}



#{i.name}



#{i.surname}




process="@this"
update="@form:display"
actionListener="#{customerMB.preprocessCustomer(i)}"
oncomplete="PF('display').show()"
/>
#{' '}
process="@this"
update="@form:display,customers"
actionListener="#{customerMB.deleteCustomer(i)}"
onclick="if(!confirm('Are You Sure?'))return false;"
/>




actionListener="#{customerMB.preprocessCustomer(null)}"
oncomplete="PF('display').show()"
process="@this"
update="@form:display"/>



modal="true" closable="true" resizable="false" >








process="display"
actionListener="#{customerMB.confirm}"
update="display,@form:customers"
oncomplete="if(#{customerMB.hide})PF('display').hide()"/>
process="@none"
update="@none"
oncomplete="PF('display').hide()"/>






8.运行Web应用

将项目打包部署到Tomcat上运行,浏览器地址栏输入http://localhost:8080/primefacesSpring/crud/main.xhtml



参考文献


1.Primefaces + Spring + Hibernate Integration Example
https://javabeat.net/primefaces-spring-hibernate-integration-example/


2.PrimeFaces Showcase
https://www.primefaces.org/showcase/


源码
https://github.com/JallenKwong/MyLab/tree/master/primefacesSpring

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台