tapestry的grid組件,具體實現方法看源碼吧,以前的教程也有講到,此次配合數據庫操做使用,實體Person.java在第二十三講中已經貼出來了,這裏就不貼了,其餘源碼以下:html
PersonCreate.javajava
/**
* 項目名稱:TapestryStart
* 開發模式:Maven+Tapestry5.x+Tapestry-hibernate+Mysql
* 版本:1.0
* 編寫:飛風
* 時間:2012-02-29
*/
package com.tapestry.app.pages.crud;
import java.util.Date;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;
import com.tapestry.app.entities.Person;
import com.tapestry.app.services.StartDAO;
public class PersonCreate {
@Property
private Person person;
//導入服務接口
@Inject
private StartDAO dao;
//初始化user實體
void onPrepare(){
person = new Person();
}
//提交表單的時候執行存儲,返回當前頁面
Object onSuccess(){
//若是時間爲空值輸入系統當前時間
if(person.getStartDate() == null){
person.setStartDate(new Date());
}
dao.create(person);
return PersonList.class;
}
}
PersonCreate.tmlsql
<html t:type="layout" title="tapestryStart Index" t:sidebarTitle="Framework Version"
xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd" xmlns:p="tapestry:parameter">
<t:form t:id="personForm">
<t:errors/>
<p>版本(version):<t:textfield t:id="version" value="person.version" t:validate="required"/></p>
<p>姓(firstName):<t:textfield t:id="firstName" value="person.firstName" t:validate="required"/></p>
<p>名(lastName):<t:textfield t:id="lastName" value="person.lastName" t:validate="required"/></p>
<p>地區(region):<t:select t:id="region" value="person.region" t:model="literal:深圳,北京" t:validate="required"/></p>
<p><input type="submit" value="建立"/><!-- <t:pagelink page="crud/UserList">返回查看頁面</t:pagelink> --></p>
</t:form>
</html>
PersonList.java數據庫
/**
* 項目名稱:TapestryStart
* 開發模式:Maven+Tapestry5.x+Tapestry-hibernate+Mysql
* 版本:1.0
* 編寫:飛風
* 時間:2012-02-29
*/
package com.tapestry.app.pages.crud;
import java.util.List;
import org.apache.tapestry5.annotations.PageActivationContext;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;
import com.tapestry.app.entities.Person;
import com.tapestry.app.services.StartDAO;
public class PersonList {
//打開user讀寫
@Property
private Person person;
//打開user陣列的讀寫
@Property
private List<Person> persons;
//導入操做數據庫的服務
@Inject
private StartDAO dao;
//當前頁面接收user的id值
@PageActivationContext
private Long id;
//頁面加載時設置渲染
void setupRender(){
//查詢User數據表
StringBuffer sql = new StringBuffer();
sql.append("from Person");
persons = dao.findWithQuery(sql.toString());
}
//單擊eventlink執行刪除操做
Object onDelete(Long id){
dao.deleteByID(Person.class, id);
return this;
}
}
PersonList.tmlapache
<html t:type="layout" title="tapestryStart Index" t:sidebarTitle="Framework Version"
xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd" xmlns:p="tapestry:parameter">
<style>
.table{border-collapse: collapse; }
.table td,table th{border:1px solid #999; padding:5px;"}
</style>
<t:pagelink page="crud/PersonCreate">添加用戶</t:pagelink><br/><br/>
<t:grid t:include="id,version,firstName,lastName,region,startDate" t:source="persons" t:row="person" t:pagerPosition="top" t:rowsPerPage="1" t:add="action" class="table">
<p:actionCell>
<t:pagelink page="crud/PersonUpdate" t:context="${person.id}">修改</t:pagelink><t:eventlink t:event="delete" t:context="${person.id}">刪除</t:eventlink>
</p:actionCell>
</t:grid>
</html>
PersonUpdate.javaapp
/**
* 項目名稱:TapestryStart
* 開發模式:Maven+Tapestry5.x+Tapestry-hibernate+Mysql
* 版本:1.0
* 編寫:飛風
* 時間:2012-02-29
*/
package com.tapestry.app.pages.crud;ide
import org.apache.tapestry5.annotations.PageActivationContext;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;
import com.tapestry.app.entities.Person;
import com.tapestry.app.services.StartDAO;
public class PersonUpdate {
//接收頁面傳來的id值
@PageActivationContext
private Long id;
//設置user可讀寫
@Property
private Person person;
//導入服務
@Inject
private StartDAO dao;
//頁面加載時運行
void onPrepare(){
//user爲空數據時根據頁面傳遞過來的id查詢數據
if(person == null){
person = dao.findByID(Person.class, id);
}
}
//提交表單保存user數據
Object onSuccess(){
dao.update(person);
return PersonList.class;
}
}
PersonUpdate.tmlui
<html t:type="layout" title="tapestryStart Index" t:sidebarTitle="Framework Version"
xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd" xmlns:p="tapestry:parameter">
<t:form>
<t:errors/>
<p>版本(version):<t:textfield t:id="version" value="person.version" t:validate="required"/></p>
<p>姓(firstName):<t:textfield t:id="firstName" value="person.firstName" t:validate="required"/></p>
<p>名(lastName):<t:textfield t:id="lastName" value="person.lastName" t:validate="required"/></p>
<p>地區(region):<t:select t:id="region" value="person.region" t:model="literal:深圳,北京" t:validate="required"/></p>
<p><input type="submit" value="保存"/><t:pagelink page="crud/PersonList">返回查看頁面</t:pagelink></p>
</t:form>
</html>
http://localhost/grid/personcrud/personlist