二手技术:
JDK 1.6.0_21
Maven的3.0.2
JSF 2.0.3 PrimeFaces 2.2.1 休眠3.6.7 MySQL Java连接器5.1.17 MySQL 5.5.8 Apache Tomcat 7.0
第1步:创建用户表
通过执行以下脚本来创建新的用户表:
CREATE TABLE USER (id int(11) NOT NULL,name varchar(45) NOT NULL,surname varchar(45) NOT NULL,PRIMARY KEY (`id`)
);
步骤2:建立已完成的专案
创建一个Maven项目,如下所示。 (可以使用Maven或IDE插件来创建它)。
步骤3:图书馆
JSF,Hibernate和依赖库添加到Maven的pom.xml中。 这些库将由Maven Central Repository下载。
<!-- JSF library -->
<dependency><groupId>com.sun.faces</groupId><artifactId>jsf-api</artifactId><version>2.0.3</version>
</dependency>
<dependency><groupId>com.sun.faces</groupId><artifactId>jsf-impl</artifactId><version>2.0.3</version>
</dependency>
<dependency><groupId>javax.servlet</groupId><artifactId>jstl</artifactId><version>1.2</version>
</dependency>
<!-- Hibernate library -->
<dependency><groupId>org.hibernate</groupId><artifactId>hibernate-core</artifactId><version>3.6.7.Final</version>
</dependency>
<dependency><groupId>javassist</groupId><artifactId>javassist</artifactId><version>3.12.1.GA</version>
</dependency>
<!-- MySQL Java Connector library -->
<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.17</version>
</dependency>
<!-- Log4j library -->
<dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.16</version>
</dependency>
注意:primefaces-2.2.1.jar也可以通过maven或以下链接下载:
<repository><id>prime-repo</id><name>PrimeFaces Maven Repository</name><url>http://repository.primefaces.org</url><layout>default</layout>
</repository> <dependency><groupId>org.primefaces</groupId><artifactId>primefaces</artifactId><version>2.2.1</version>
</dependency>
要么
http://www.primefaces.org/downloads.html
第4步:创建受管理的豆类
创建一个新的托管bean类。 使用了可以与UI组件关联的bean。 托管Bean包含属性以及getter和setter方法。 此外,它们可以涵盖事件处理,导航,验证等方法。
package com.otv;import java.io.Serializable;
import java.util.List;import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.Transaction;import com.otv.hbm.User;
import com.otv.util.HibernateUtil;/*** @author onlinetechvision.com* @since 3 Oct 2011* @version 1.0.0**/
public class UserManagedBean implements Serializable{private static final long serialVersionUID = 1L;private static Logger log = Logger.getLogger(UserManagedBean.class);private static final String SUCCESS = 'success';private static final String ERROR = 'error';private String name;private String surname;private String message;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getSurname() {return surname;}public void setSurname(String surname) {this.surname = surname;}public String getMessage() {StringBuffer strBuff = new StringBuffer();strBuff.append('Name : ').append(this.getName());strBuff.append(', Surname : ').append(this.getSurname());this.setMessage(strBuff.toString());return this.message;}public void setMessage(String message) {this.message = message;} public String save() {String result = null;Session session = HibernateUtil.getSessionFactory().openSession();User user = new User();user.setName(this.getName());user.setSurname(this.getSurname());Transaction tx = null;try {tx = session.beginTransaction();session.save(user);tx.commit();log.debug('New Record : ' + user + ', wasCommitted : ' + tx.wasCommitted());result = SUCCESS;} catch (Exception e) {if (tx != null) {tx.rollback();result = ERROR;e.printStackTrace();}} finally {session.close();}return result;}public List<User> getUsers() {Session session = HibernateUtil.getSessionFactory().openSession();List<User> userList = session.createCriteria(User.class).list();return userList;}public void reset() {this.setName('');this.setSurname('');}
}
第5步:创建用户类别
创建一个新的User类以对User Table建模。
package com.otv.hbm;
/*** @author onlinetechvision.com* @since 3 Oct 2011* @version 1.0.0**/
public class User {private int id;private String name;private String surname;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getSurname() {return surname;}public void setSurname(String surname) {this.surname = surname;} @Overridepublic String toString() {StringBuffer strBuff = new StringBuffer();strBuff.append('id : ').append(id);strBuff.append(', name : ').append(name);strBuff.append(', surname : ').append(surname);return strBuff.toString();}
}
第6步:创建卫生保健课
创建Singleton HibernateUtil类以构建Hibernate SessionFactory对象。
package com.otv.util;import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;/*** @author onlinetechvision.com* @since 3 Oct 2011* @version 1.0.0**/
public class HibernateUtil {private static SessionFactory sessionFactory = null;public static SessionFactory getSessionFactory() {if(sessionFactory == null) {sessionFactory = new Configuration().configure().buildSessionFactory();}return sessionFactory;}public static void setSessionFactory(SessionFactory sessionFactory) {HibernateUtil.sessionFactory = sessionFactory;}}
步骤7:创建index.xhtml
index.xhtml已创建。
<html xmlns='http://www.w3.org/1999/xhtml'xmlns:h='http://java.sun.com/jsf/html'xmlns:f='http://java.sun.com/jsf/core'xmlns:p='http://primefaces.prime.com.tr/ui'> <h:head><title>Welcome to JSF_PrimeFaces_Hibernate Project</title></h:head><body><h:form><table><tr><td><h:outputLabel for='name' value='Name:' /></td><td><p:inputText id='name' value='#{userMBean.name}'/></td></tr><tr><td><h:outputLabel for='surname' value='Surname:' /></td><td><p:inputText id='surname' value='#{userMBean.surname}'/> </td></tr><tr><td><p:commandButton id='submit' value='Save' action='#{userMBean.save}' ajax='false'/></td><td><p:commandButton id='reset' value='Reset' action='#{userMBean.reset}' ajax='false'/></td></tr></table></h:form>
</body>
</html>
第8步:创建welcome.xhtml
welcome.xhtml已创建。
<html xmlns='http://www.w3.org/1999/xhtml'xmlns:h='http://java.sun.com/jsf/html'xmlns:f='http://java.sun.com/jsf/core'xmlns:p='http://primefaces.prime.com.tr/ui'><h:head><title>Welcome to JSF_PrimeFaces_Hibernate Project</title>
</h:head>
<body><h:form><h:outputText value='Saved Record is #{userMBean.message}'></h:outputText><p:dataTable id='users' value='#{userMBean.getUsers()}' var='user' style='width: 10%'><p:column><f:facet name='header'><h:outputText value='ID' /></f:facet><h:outputText value='#{user.id}' /></p:column><p:column><f:facet name='header'><h:outputText value='Name' /></f:facet><h:outputText value='#{user.name}' /></p:column><p:column><f:facet name='header'><h:outputText value='Surname' /></f:facet><h:outputText value='#{user.surname}' /></p:column></p:dataTable></h:form>
</body>
</html>
步骤9:创建error.xhtml
error.xhtml已创建。
<html xmlns='http://www.w3.org/1999/xhtml'xmlns:h='http://java.sun.com/jsf/html'xmlns:f='http://java.sun.com/jsf/core'xmlns:p='http://primefaces.prime.com.tr/ui'> <h:head><title>Welcome to JSF_PrimeFaces_Hibernate Project</title></h:head><body><f:view><h:form><h:outputText value='Transaction Error has occurred!'></h:outputText></h:form></f:view>
</body>
</html>
步骤10:配置faces-config.xml
faces-config.xml的创建如下。 它涵盖了托管Bean的配置以及xhtml页面之间的导航。
<?xml version='1.0' encoding='UTF-8'?>
<faces-configxmlns='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'><managed-bean><managed-bean-name>userMBean</managed-bean-name><managed-bean-class>com.otv.UserManagedBean</managed-bean-class><managed-bean-scope>request</managed-bean-scope></managed-bean><navigation-rule><from-view-id>/pages/index.xhtml</from-view-id><navigation-case><from-outcome>success</from-outcome><to-view-id>/pages/welcome.xhtml</to-view-id></navigation-case><navigation-case><from-outcome>error</from-outcome><to-view-id>/pages/error.xhtml</to-view-id></navigation-case></navigation-rule>
</faces-config>
步骤11:更新web.xml
web.xml已更新。
<?xml version='1.0' encoding='UTF-8'?>
<web-app 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'><display-name>OTV_JSF_PrimeFaces_Hibernate</display-name><context-param><param-name>javax.faces.PROJECT_STAGE</param-name><param-value>Development</param-value></context-param><welcome-file-list><welcome-file>/pages/index.xhtml</welcome-file></welcome-file-list><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>/faces/*</url-pattern></servlet-mapping><servlet-mapping><servlet-name>Faces Servlet</servlet-name><url-pattern>*.jsf</url-pattern></servlet-mapping><servlet-mapping><servlet-name>Faces Servlet</servlet-name><url-pattern>*.faces</url-pattern></servlet-mapping><servlet-mapping><servlet-name>Faces Servlet</servlet-name><url-pattern>*.xhtml</url-pattern></servlet-mapping>
</web-app>
步骤12:建立user.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.otv.hbm.User' table='USER'><id name='id' type='int' column='ID' ><generator class='increment'/></id><property name='name'><column name='NAME' /></property><property name='surname'><column name='SURNAME'/></property></class>
</hibernate-mapping>
步骤13:建立hibernate.cfg.xml
创建hibernate.cfg.xml来管理应用程序和数据库之间的交互:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC'-//Hibernate/Hibernate Configuration DTD//EN''http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd'><hibernate-configuration>
<session-factory><property name='hibernate.connection.driver_class'>com.mysql.jdbc.Driver</property><property name='hibernate.connection.url'>jdbc:mysql://localhost:3306/Test</property><property name='hibernate.connection.username'>root</property><property name='hibernate.connection.password'>root</property><property name='hibernate.connection.pool_size'>10</property><property name='show_sql'>true</property><property name='dialect'>org.hibernate.dialect.MySQLDialect</property><!-- Mapping files --><mapping resource='hbm/user.hbm.xml'/>
</session-factory>
</hibernate-configuration>
步骤14:将项目部署到应用程序服务器
将Project部署到Application Server(Apache tomcat)时,屏幕如下所示:
单击提交按钮后,将显示welcome.xhtml页面,如下所示:
步骤15:下载
OTV_JSF_Hibernate_PrimeFaces
参考: JSF –我们的JCG合作伙伴 Eren Avsarogullari的PrimeFaces和Hibernate集成项目 ,位于Online Technology Vision博客上。
翻译自: https://www.javacodegeeks.com/2012/08/jsf-primefaces-hibernate-integration.html