第三讲:tapestry增删改查---查询数据

思路:使用loop组件循环显示每条增加的数据,添加用户使用了pagelink。

 

在com.tapestry.app.pages.crud包里创建UserList.java,在webapp/crud里创建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.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;
 
//页面加载时设置渲染
void setupRender(){
//查询User数据表
StringBuffer sql = new StringBuffer();
sql.append("from User");
users = dao.findWithQuery(sql.toString());
}
 
 
 
}
 

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.time}</td>
    <td>${user.id}</td>
     <td></td>
  </tr>
</table>
</html>

修改下UserCreate.java与UserCreate.tml文件,实现添加完成之后,直接跳转到UserList页面

UserCreate.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.Date;
 
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 UserCreate {
 
@Property
private User user;
 
//导入服务接口
@Inject
private StartDAO dao;
 
//初始化user实体
void onPrepare(){
user = new User();
}
 
//提交表单的时候执行存储,返回当前页面
Object onSuccess(){
//如果时间为空值输入系统当前时间
if(user.getTime() == null){
user.setTime(new Date());
}
dao.create(user);
return UserList.class;
}
}
 

UserCreate.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增删改查---查询数据)