第四讲:tapestry增删改查---修改数据

思路:在UserList页面传递user的id到修改也UesrUpdate页面,再查询出所有对应的数据。

 

修改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></td>
  </tr>
</table>
</html>

在com.tapestry.app.pages.crud包里添加UserUpdate.java,在webapp/crud下增加UserUpdate.tml代码如下:

UserUpdate.java

/**
* 项目名称:TapestryStart
* 开发模式:Maven+Tapestry5.x+Tapestry-hibernate+Mysql
* 网址: http://www.flywind.org
* 版本: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.User;
import com.tapestry.app.services.StartDAO;
 
public class UserUpdate {
 
//接收页面传来的id值
@PageActivationContext
private Long id;
 
//设置user可读写
@Property
private User user;
 
//导入服务
@Inject
private StartDAO dao;
 
//页面加载时运行
void onPrepare(){
//user为空数据时根据页面传递过来的id查询数据
if(user == null){
user = dao.findByID(User.class, id);
}
}
//提交表单保存user数据
Object onSuccess(){
dao.update(user);
return UserList.class;
}
}
 

UserUpdate.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>用户名:<t:textfield t:id="name" value="user.name" t:validate="required"/></p>
  <p>年龄:<t:textfield t:id="age" value="user.age" t:validate="required"/></p>
  <p><input type="submit" value="保存"/><t:pagelink page="crud/UserList">返回查看页面</t:pagelink></p>
 </t:form>
 </html>

查看http://localhost/crud/userlist修改数据也做好了。

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