第二十四讲:tapestry的grid组件

tapestry的grid组件,具体实现方法看源码吧,之前的教程也有讲到,这次配合数据库操作使用,实体Person.java在第二十三讲中已经贴出来了,这里就不贴了,其他源码如下:

PersonCreate.java

/**
* 项目名称: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.tml

<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.tml

<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.java

/**
* 项目名称:TapestryStart
* 开发模式:Maven+Tapestry5.x+Tapestry-hibernate+Mysql
* 版本:1.0
* 编写:飞风
* 时间:2012-02-29

*/ 
package com.tapestry.app.pages.crud;

 
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.tml

<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

转载于:https://my.oschina.net/shootercn/blog/53698

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值