- foxgst
- 等级: 初级会员

- 文章: 45
- 积分: 10

|
最后更新时间:2007-11-13 关键字: Tapestry,Grid
正在学习Tapestry中,根据搜索的资料和情况发布学习心得,目前使用的Tapestry版本是5.0.6。
参考资料:
1. 主题: T5 技巧 2:为Grid组件的每一行增加“删除/修改”链接。
针对T5.0.5版本,方法稍微有点复杂(Order排序也已经过时)。目前版本是5.0.6,另给出了一个通用的简便的方法。
2. Adding modify/delete actions to a Grid
虽然贴的是代码,但是对于在网页中如何使用没有给出实现。
原理:
dengyin2000已经把原理说得很清楚了,扩展BeanModelSource以实现复杂的表格(Grid)显示。
操作过程:
1. 创建需要的PropertyConduit,也就是新列(可以复用显示多列);
2. 修改Page类,增加Model的获取方法;
3. 修改相应网页,使用Grid显示数据(需要注意的是,新建列的显示需加入自定义的代码实现)。
主要代码:
1. 新建需要的Property
java 代码
-
- class ActionProperty implements PropertyConduit {
- public Object get(Object arg0) {return "";}
- public Class getPropertyType() {return String.class;}
- public void set(Object arg0, Object arg1) {}
- public extends Annotation> T getAnnotation(Class arg0) {return null;}
- }
2. 修改Page类User,新增代码如下:
java 代码
- @Inject
- private BeanModelSource _modelSource;
- @Inject
- private ComponentResources _resources;
-
- public BeanModel getModelForGrid() {
- PropertyConduit ap = new ActionProperty();
- BeanModel result = _modelSource.create(User.class, false, _resources);
- result.add("action", ap).label("操作").sortable(false);
- // 你也可以使用如下方法把action列放在uuid(在User类中必须要有可访问的getUuid()方法)列的前面
- // result.add(RelativePosition.BEFORE, "uuid", "action", ap).label("操作").sortable(false);
- // 你也可以复用ap再加入新列
- // result.add("chk", ap).label("选择").sortable(false);
- return result;
- }
3. 修改相应网页UserList.tml,使用Grid的代码如下:
xml 代码
- <table t:type="Grid" model="modelForGrid" source="users" row="currentUser" pagerPosition="bottom" rowsPerPage="5">
- <t:parameter t:name="actionCell">
- <a t:type="ActionLink" t:id="delete" context="currentUser.uuid">删除a> <a t:type="ActionLink" t:id="modify" context="currentUser.uuid">修改a>
- >
- >
部分完整代码
1. Page类UserList.java
java 代码
- import java.util.List;
-
- import org.apache.tapestry.Block;
- import org.apache.tapestry.ComponentResources;
- import org.apache.tapestry.PropertyConduit;
- import org.apache.tapestry.beaneditor.BeanModel;
- import org.apache.tapestry.ioc.annotations.Inject;
- import org.apache.tapestry.services.BeanModelSource;
-
- import com.senlang.sample.tapestryfive.demo.model.User;
- import com.senlang.sample.tapestryfive.demo.service.UserService;
- import com.senlang.sample.tapestryfive.demo.util.ActionProperty;
-
- public class UserList {
-
- @Inject
- private Block _noData;
-
- @Inject
- private UserService _usrv;
-
- private User _currentUser;
-
- @Inject
- private BeanModelSource _modelSource;
-
- @Inject
- private ComponentResources _resources;
-
- public List getUsers() {
- return _usrv.getAllUser();
- }
-
- public BeanModel getModelForGrid(){
- PropertyConduit ap = new ActionProperty();
- BeanModel result = _modelSource.create(User.class, false, _resources);
- result.add("action", ap).label("操作").sortable(false);
-
-
-
-
- return result;
- }
-
-
- Object onActionFromDelete(String uuid) {
- System.out.println(String.format("delete uuid=%s", uuid));
- return null;
- }
-
-
- Object onActionFromModify(String uuid) {
- System.out.println(String.format("modify uuid=%s", uuid));
- return null;
- }
-
- public Block getNoData() {
- return _noData;
- }
-
- public void setNoData(Block noData) {
- _noData = noData;
- }
-
- public UserService getUsrv() {
- return _usrv;
- }
-
- public void setUsrv(UserService usrv) {
- _usrv = usrv;
- }
-
- public User getCurrentUser() {
- return _currentUser;
- }
-
- public void setCurrentUser(User currentUser) {
- _currentUser = currentUser;
- }
-
- public BeanModelSource getModelSource() {
- return _modelSource;
- }
-
- public void setModelSource(BeanModelSource modelSource) {
- _modelSource = modelSource;
- }
-
- public ComponentResources getResources() {
- return _resources;
- }
-
- public void setResources(ComponentResources resources) {
- _resources = resources;
- }
-
- }
2. 网页UserList.tml代码
xml 代码
-
- <html xmlns="http://www.w3.org/1999/xhtml" xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>Samples - User Listtitle>
- >
- <body>
- <t:block id="nodata">没有找到数据!t:block>
- <h1>List All Usersh1>
- <table t:type="Grid" model="modelForGrid" source="users"
- row="currentUser" pagerPosition="bottom" rowsPerPage="5" empty="nodata">
- <t:parameter t:name="actionCell">
- <a t:type="ActionLink" t:id="delete" context="currentUser.uuid">删除<a>
- <a t:type="ActionLink" t:id="modify" context="currentUser.uuid">修改<a>
- t:parameter>
3. User类的实现代码
java 代码
- public class User {
- private String _uuid;
- private String _username;
- private String _password;
- public String getUuid() {
- return _uuid;
- }
- public void setUuid(String uuid) {
- _uuid = uuid;
- }
- public String getUsername() {
- return _username;
- }
- public void setUsername(String username) {
- _username = username;
- }
- public String getPassword() {
- return _password;
- }
- public void setPassword(String password) {
- _password = password;
- }
- }
声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
| 返回顶楼 |
|
|
- foxgst
- 等级: 初级会员

- 文章: 45
- 积分: 10

|
JavaEye的编辑器不是一般的难用,整了十几次才贴成这样。
现在的代码还是有问题,不想整了,大致意思已经说明白了,有问题请回复吧。
|
| 返回顶楼 |
|
|
- foxgst
- 等级: 初级会员

- 文章: 45
- 积分: 10

|
主要是网页代码显示的问题,使用xml格式居然不对,贴张截图。

- 描述: UserList.tml截图
- 大小: 26.5 KB
|
| 返回顶楼 |
|
|
- hitalang
- 等级: 初级会员

- 性别:

- 文章: 12
- 积分: 0
- 来自: 杭州

|
t5.05中datafield,时间控件怎么用???
|
| 返回顶楼 |
|
|