Apache Tapestry 休眠
在本章中,我们将讨论集成 BeanEditForm and 网格组件 与休眠。 Hibernate 通过 hibernate 模块集成到挂毯中。要启用休眠模块,请添加 Tapestry-hibernate 依赖项和可选 hsqldb in the pom.xml 文件。现在,通过 休眠.cfg.xml 文件放在资源文件夹的根目录下。
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>
休眠.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC "-// 休眠/休眠配置 DTD 3.0 "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 组件和 员工列表页面 使用网格组件。持久层由 Hibernate 模块处理。
创建一个员工类并用@Entity 注解装饰它。然后,为相关字段添加验证注解,为 id 字段添加休眠相关注解@Id 和@GeneratedValue。此外,将性别创建为枚举类型。
雇员.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 }
创建员工列表页面, ListEmployee.java 在 pages 下的新文件夹 employee 和对应的模板文件 ListEmployee.tml 中 /src/main/resources/pages/员工 文件夹。 Tapestry 通过删除重复数据为子文件夹提供了一个短 URL。
例如,可以通过普通 URL – (/employee/listemployee) 和短 URL – (/employee/list) 访问 ListEmployee 页面。
使用 @Inject 注释将 Hibernate 会话注入列表页面。定义一个属性 得到雇员 在列表页面中,并使用注入的会话对象填充员工。完成员工类的代码,如下所示。
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 : 创建员工链接页面。
-
Grid : 用于渲染员工详细信息。网格组件具有用于注入员工列表的源属性并包含用于包含要呈现的字段的属性。
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 组件。该组件具有以下属性:
-
object : 包括来源。
-
reorder :定义要渲染的字段的顺序。
-
提交标签 : 表单提交按钮的消息
完整的编码如下:
<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 事件(更新数据的地方)。会话数据使用休眠会话持久化到数据库中。
完整的编码如下:
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; } }
Add the CreateEmployee.properties 文件并包含要在表单验证中使用的消息。完整代码如下:
zip-regexp=^\\d{5}(-\\d{4})?$ zip-regexp-message = Zip Codes are five or nine digits. 例子: 02134 or 901251655. submit-label = Create Employee
员工创建页面和列表页面截图如下: