第五讲:tapestry增删改查---删除数据

思路:根据user的id去删除数据,直接从UserList页面实现删除。

修改UserList.java,与UserList.tml文件。代码如下:

UserList.java

/**
* 项目名称:TapestryStart
* 开发模式:Maven+Tapestry5.x+Tapestry-hibernate+Mysql
* 网址: http://www.flywind.org
* 版本: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.User;
import com.tapestry.app.services.StartDAO;
 
public class UserList {
 
//打开user读写
@Property
private User user;
 
//打开user阵列的读写
@Property
private List<User> users;
 
//导入操作数据库的服务
@Inject
private StartDAO dao;
 
//当前页面接收user的id值
@PageActivationContext
private Long id;
 
//页面加载时设置渲染
void setupRender(){
//查询User数据表
StringBuffer sql = new StringBuffer();
sql.append("from User");
users = dao.findWithQuery(sql.toString());
}
 
//单击eventlink执行删除操作
Object onDelete(Long id){
dao.deleteByID(User.class, id);
return this;
}
 
}
 

UserList.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/UserCreate">添加用户</t:pagelink><br/><br/>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="table">
  <tr>
    <th scope="col">id</th>
    <th scope="col">用户名</th>
    <th scope="col">年龄</th>
    <th scope="col">时间</th>
    <th scope="col">操作</th>
  </tr>
  <tr t:type="loop" t:source="users" t:value="user">
    <td>${user.id}</td>
    <td>${user.name}</td>
    <td>${user.age}</td>
    <td>${user.time}</td>
    <td><t:pagelink page="crud/UserUpdate" t:context="${user.id}">修改</t:pagelink> | <t:eventlink t:event="delete" t:context="${user.id}">删除</t:eventlink></td>
  </tr>
</table>
</html>

查看http://localhost/crud/userlist删除已经做好了

你可能感兴趣的:(第五讲:tapestry增删改查---删除数据)