Apache-tapestry-hibernate
Apacheタペストリー-休止状態
この章では、 BeanEditForm および* Gridコンポーネント*とHibernateの統合について説明します。 Hibernateはhibernateモジュールを介してタペストリーに統合されています。 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コンポーネントを使用して* employee追加ページ*を作成し、Gridコンポーネントを使用して* employeeリストページ*を作成する方法を見てみましょう。 永続層はHibernateモジュールによって処理されます。
従業員クラスを作成し、@ Entityアノテーションで装飾します。 次に、関連フィールドに検証アノテーションを追加し、idフィールドに関連アノテーション@Idおよび@GeneratedValueを休止状態にします。 また、性別を列挙型として作成します。
Employee.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 を、ページの下の新しいフォルダemployeeおよび /src/main/resources/pages/employee フォルダにある対応するテンプレートファイルListEmployee.tmlに作成します。 Tapestryは、繰り返しデータを削除することにより、サブフォルダーの短いURLを提供します。
たとえば、ListEmployeeページには、通常のURL(/employee/listemployee)および短縮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クラスのテンプレートファイルを作成します。 テンプレートには2つの主要なコンポーネントがあります-
- PageLink -従業員のリンクページを作成します。
- グリッド-従業員の詳細を表示するために使用されます。 グリッドコンポーネントには、従業員リストを挿入するソース属性があり、レンダリングするフィールドを含める属性が含まれています。
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
従業員の作成ページとリストページのスクリーンショットは以下に示されています-