论坛首页 Java版 企业应用

T5.0.6 扩展Grid列的方法

浏览 712 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
最后更新时间: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 代码
 
  1. //PropertyConduit CLASS - implements the PropertyConduit Interface  
  2. class ActionProperty implements PropertyConduit {  
  3. public Object get(Object arg0) {return "";}  
  4. public Class getPropertyType() {return String.class;}  
  5. public void set(Object arg0, Object arg1) {}  
  6. public extends Annotation> T getAnnotation(Class arg0) {return null;}  
  7. }  

 2. 修改Page类User,新增代码如下:
java 代码
 
  1. @Inject  
  2. private BeanModelSource _modelSource;  
  3. @Inject  
  4. private ComponentResources _resources;  
  5.   
  6. public BeanModel getModelForGrid() {  
  7. PropertyConduit ap = new ActionProperty();  
  8. BeanModel result = _modelSource.create(User.classfalse, _resources);
  9. result.add("action", ap).label("操作").sortable(false);  
  10. // 你也可以使用如下方法把action列放在uuid(在User类中必须要有可访问的getUuid()方法)列的前面
  11. // result.add(RelativePosition.BEFORE, "uuid", "action", ap).label("操作").sortable(false); 
  12. // 你也可以复用ap再加入新列
  13. // result.add("chk", ap).label("选择").sortable(false);  
  14. return result;  
  15. }  


3. 修改相应网页UserList.tml,使用Grid的代码如下:

xml 代码
  1. <table t:type="Grid" model="modelForGrid" source="users" row="currentUser" pagerPosition="bottom" rowsPerPage="5">  
  2. <t:parameter t:name="actionCell">  
  3. <a t:type="ActionLink" t:id="delete" context="currentUser.uuid">删除a>     <a t:type="ActionLink" t:id="modify" context="currentUser.uuid">修改a>  
  4. >  
  5. >  

 部分完整代码

1. Page类UserList.java

java 代码
  1. import java.util.List;   
  2.   
  3. import org.apache.tapestry.Block;   
  4. import org.apache.tapestry.ComponentResources;   
  5. import org.apache.tapestry.PropertyConduit;   
  6. import org.apache.tapestry.beaneditor.BeanModel;   
  7. import org.apache.tapestry.ioc.annotations.Inject;   
  8. import org.apache.tapestry.services.BeanModelSource;   
  9.   
  10. import com.senlang.sample.tapestryfive.demo.model.User;   
  11. import com.senlang.sample.tapestryfive.demo.service.UserService;   
  12. import com.senlang.sample.tapestryfive.demo.util.ActionProperty;   
  13.   
  14. public class UserList {   
  15.        
  16.     @Inject  
  17.     private Block _noData;   
  18.        
  19.     @Inject  
  20.     private UserService _usrv;   
  21.        
  22.     private User _currentUser;   
  23.        
  24.     @Inject     
  25.     private BeanModelSource _modelSource;   
  26.        
  27.     @Inject     
  28.     private ComponentResources _resources;     
  29.        
  30.     public List getUsers() {   
  31.         return _usrv.getAllUser();   
  32.     }   
  33.        
  34.     public BeanModel getModelForGrid(){    
  35.         PropertyConduit ap = new ActionProperty();      
  36.         BeanModel result = _modelSource.create(User.classfalse, _resources);    
  37.         result.add("action", ap).label("操作").sortable(false);      
  38.         // 你也可以使用如下方法把action列放在uuid(在User类中必须要有可访问的getUuid()方法)列的前面    
  39.         // result.add(RelativePosition.BEFORE, "uuid", "action", ap).label("操作").sortable(false);     
  40.         // 你也可以服用ap再加入新列    
  41.         // result.add("chk", ap).label("选择").sortable(false);      
  42.         return result;   
  43.     }   
  44.        
  45.     // 删除操作   
  46.     Object onActionFromDelete(String uuid) {   
  47.         System.out.println(String.format("delete uuid=%s", uuid));   
  48.         return null;   
  49.     }   
  50.        
  51.     // 修改操作   
  52.     Object onActionFromModify(String uuid) {   
  53.         System.out.println(String.format("modify uuid=%s", uuid));   
  54.         return null;   
  55.     }   
  56.   
  57.     public Block getNoData() {   
  58.         return _noData;   
  59.     }   
  60.   
  61.     public void setNoData(Block noData) {   
  62.         _noData = noData;   
  63.     }   
  64.   
  65.     public UserService getUsrv() {   
  66.         return _usrv;   
  67.     }   
  68.   
  69.     public void setUsrv(UserService usrv) {   
  70.         _usrv = usrv;   
  71.     }   
  72.   
  73.     public User getCurrentUser() {   
  74.         return _currentUser;   
  75.     }   
  76.   
  77.     public void setCurrentUser(User currentUser) {   
  78.         _currentUser = currentUser;   
  79.     }   
  80.   
  81.     public BeanModelSource getModelSource() {   
  82.         return _modelSource;   
  83.     }   
  84.   
  85.     public void setModelSource(BeanModelSource modelSource) {   
  86.         _modelSource = modelSource;   
  87.     }   
  88.   
  89.     public ComponentResources getResources() {   
  90.         return _resources;   
  91.     }   
  92.   
  93.     public void setResources(ComponentResources resources) {   
  94.         _resources = resources;   
  95.     }   
  96.        
  97. }  

2. 网页UserList.tml代码

xml 代码
  1.   
  2. <html xmlns="http://www.w3.org/1999/xhtml" xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  5. <title>Samples - User Listtitle>  
  6. >  
  7. <body>  
  8. <t:block id="nodata">没有找到数据!t:block>  
  9. <h1>List All Usersh1>  
  10. <table t:type="Grid" model="modelForGrid" source="users"  
  11.     row="currentUser" pagerPosition="bottom" rowsPerPage="5" empty="nodata">  
  12.     <t:parameter t:name="actionCell">  
  13.         <a t:type="ActionLink" t:id="delete" context="currentUser.uuid">删除<a>  
  14.         <a t:type="ActionLink" t:id="modify" context="currentUser.uuid">修改<a>  
  15.     t:parameter>  

 3. User类的实现代码

java 代码
  1. public class User {   
  2.     private String _uuid;   
  3.     private String _username;   
  4.     private String _password;   
  5.     public String getUuid() {   
  6.         return _uuid;   
  7.     }   
  8.     public void setUuid(String uuid) {   
  9.         _uuid = uuid;   
  10.     }   
  11.     public String getUsername() {   
  12.         return _username;   
  13.     }   
  14.     public void setUsername(String username) {   
  15.         _username = username;   
  16.     }   
  17.     public String getPassword() {   
  18.         return _password;   
  19.     }   
  20.     public void setPassword(String password) {   
  21.         _password = password;   
  22.     }   
  23. }  
   
最后更新时间:2007-11-13
JavaEye的编辑器不是一般的难用,整了十几次才贴成这样。
现在的代码还是有问题,不想整了,大致意思已经说明白了,有问题请回复吧。
   
0 请登录后投票
最后更新时间:2007-11-13
主要是网页代码显示的问题,使用xml格式居然不对,贴张截图。
  • 62f88bca-dab7-4e67-a5d3-1ce11744120b-thumb
  • 描述: UserList.tml截图
  • 大小: 26.5 KB
   
0 请登录后投票
最后更新时间:2007-12-02
t5.05中datafield,时间控件怎么用???
   
0 请登录后投票
论坛首页 Java版 企业应用

跳转论坛:
JavaEye推荐