- Apache Tapestry 教程
- Apache Tapestry - 主页
- Apache Tapestry - 概述
- Apache Tapestry - 架构
- Apache Tapestry - 安装
- Apache Tapestry - 快速入门
- Apache Tapestry - 项目布局
- 约定优于配置
- Apache Tapestry - 注释
- 页面和组件
- Apache Tapestry - 模板
- Apache Tapestry - 组件
- 内置组件
- 表单和验证组件
- Apache Tapestry - Ajax 组件
- Apache Tapestry - Hibernate
- Apache Tapestry - 存储
- 高级功能
- Apache Tapestry 有用资源
- Apache Tapestry - 快速指南
- Apache Tapestry - 有用的资源
- Apache Tapestry - 讨论
Apache Tapestry - Hibernate
在本章中,我们将讨论BeanEditForm和Grid 组件与 Hibernate的集成。Hibernate 通过 hibernate 模块集成到 Tapestry 中。要启用 hibernate 模块,请在pom.xml文件中添加 Tapestry-hibernate 依赖项和可选的hsqldb。现在,通过位于资源文件夹根目录下的hibernate.cfg.xml文件配置 hibernate 。
pom.xml(部分)
<dependency> <groupId>org.apache.tapestry</groupId> <artifactId>tapestry-hibernate</artifactId> <version>${tapestry-release-version}</version> </dependency> <dependency> <groupId>org.hsqldb</groupId> <artifactId>hsqldb</artifactId> <version>2.3.2</version> </dependency>
Hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name = "hibernate.connection.driver_class"> org.hsqldb.jdbcDriver </property> <property name = "hibernate.connection.url"> jdbc:hsqldb:./target/work/sampleapp;shutdown = true </property> <property name = "hibernate.dialect"> org.hibernate.dialect.HSQLDialect </property> <property name = "hibernate.connection.username">sa</property> <property name = "hibernate.connection.password"></property> <property name = "hbm2ddl.auto">update</property> <property name = "hibernate.show_sql">true</property> <property name = "hibernate.format_sql">true</property> </session-factory> </hibernate-configuration>
让我们看看如何使用 BeanEditForm 组件创建员工添加页面和使用 Grid 组件创建员工列表页面。持久层由 Hibernate 模块处理。
创建一个员工类并用@Entity注解装饰它。然后,为相关字段添加验证注解,为id字段添加hibernate相关注解@Id和@GenerateValue。另外,将性别创建为枚举类型。
员工.java
package com.example.MyFirstApplication.entities; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import org.apache.tapestry5.beaneditor.NonVisual; import org.apache.tapestry5.beaneditor.Validate; @Entity public class Employee { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @NonVisual public Long id; @Validate("required") public String firstName; @Validate("required") public String lastName; @Validate("required") public String userName; @Validate("required") public String password; @Validate("required") public String email; public String phone; @Validate("required") public String Street; @Validate("required") public String city; @Validate("required") public String state; @Validate("required,regexp=^\\d{5}(-\\d{4})?$") public String zip; } Gender.java (enum) package com.example.MyFirstApplication.data; public enum Gender { Male, Female }
创建员工列表页面,在pages下新建文件夹employee中创建ListEmployee.java ,并在/src/main/resources/pages/employee文件夹中创建相应的模板文件ListEmployee.tml 。Tapestry 通过删除重复数据为子文件夹提供短 URL。
例如,ListEmployee 页面可以通过普通 URL(/employee/listtemployee)和短 URL(/employee/list)访问。
使用@Inject注释将Hibernate会话注入列表页面。在列表页面中定义一个属性getEmployees并使用注入的会话对象将员工填充到其中。完成员工类别的代码,如下所示。
ListEmployee.java
package com.example.MyFirstApplication.pages.employee; import java.util.List; import org.apache.tapestry5.annotations.Import; import org.apache.tapestry5.ioc.annotations.Inject; import org.hibernate.Session; import com.example.MyFirstApplication.entities.Employee; import org.apache.tapestry5.annotations.Import; @Import(stylesheet="context:mybootstrap/css/bootstrap.css") public class ListEmployee { @Inject private Session session; public List<Employee> getEmployees() { return session.createCriteria(Employee.class).list(); } }
为 ListEmployee 类创建模板文件。该模板将有两个主要组件,它们是 -
PageLink - 创建员工链接页面。
网格- 用于呈现员工详细信息。网格组件具有用于注入员工列表的sources属性和用于包含要呈现的字段的include属性。
ListEmployee.tml(列出所有员工)
<html t:type = "simplelayout" title = "List Employee" xmlns:t = "http://tapestry.apache.org/schema/tapestry_5_4.xsd"> <h1>Employees</h1> <ul> <li><t:pagelink page = "employee/create">Create new employee</t:pagelink></li> </ul> <t:grid source = "employees" include = "userName,firstName,lastName,gender,dateOfBirth,phone,city,state"/> </html>
创建员工创建模板文件并包含 BeanEditForm 组件。该组件具有以下属性 -
对象- 包括源。
reorder - 定义要呈现的字段的顺序。
Submitlabel - 表单提交按钮的消息
完整的编码如下 -
<html t:type = "simplelayout" title = "Create New Address" xmlns:t = "http://tapestry.apache.org/schema/tapestry_5_4.xsd"> <t:beaneditform object = "employee" submitlabel = "message:submit-label" reorder = "userName,password,firstName,lastName, dateOfBirth,gender,email,phone,s treet,city,state,zip" /> </html>
创建员工创建类并包含会话、员工属性、列表页面(导航链接)并定义组件的 OnSuccess 事件(更新数据的位置)。会话数据使用 hibernate 会话保存到数据库中。
完整的编码如下 -
package com.example.MyFirstApplication.pages.employee; import com.example.MyFirstApplication.entities.Employee; import com.example.MyFirstApplication.pages.employee.ListEmployee; import org.apache.tapestry5.annotations.InjectPage; import org.apache.tapestry5.annotations.Property; import org.apache.tapestry5.hibernate.annotations.CommitAfter; import org.apache.tapestry5.ioc.annotations.Inject; import org.hibernate.Session; public class CreateEmployee { @Property private Employee employee; @Inject private Session session; @InjectPage private ListEmployee listPage; @CommitAfter Object onSuccess() { session.persist(employee); return listPage; } }
添加CreateEmployee.properties文件并包含要在表单验证中使用的消息。完整代码如下 -
zip-regexp=^\\d{5}(-\\d{4})?$ zip-regexp-message = Zip Codes are five or nine digits. Example: 02134 or 901251655. submit-label = Create Employee
员工创建页面和列表页面的屏幕截图如下所示 -