Tapestry5 BeanModel分页实例

1、首先在实体包下建立实体类Book,代码如下:

java 代码
  1.   
  2. public class Book {   
  3.     private String bookName;   
  4.     private String author;   
  5.     private Double price;   
  6.     private String bookConcern;   
  7.     private Date printTime;   
  8.     private String beDeleted;   
  9.     public String getBeDeleted() {   
  10.         return beDeleted;   
  11.     }   
  12.     public void setBeDeleted(String beDeleted) {   
  13.         this.beDeleted = beDeleted;   
  14.     }   
  15.     public String getAuthor() {   
  16.         return author;   
  17.     }   
  18.     public void setAuthor(String author) {   
  19.         this.author = author;   
  20.     }   
  21.     public String getBookConcern() {   
  22.         return bookConcern;   
  23.     }   
  24.     public void setBookConcern(String bookConcern) {   
  25.         this.bookConcern = bookConcern;   
  26.     }   
  27.     public String getBookName() {   
  28.         return bookName;   
  29.     }   
  30.     public void setBookName(String bookName) {   
  31.         this.bookName = bookName;   
  32.     }   
  33.     public Double getPrice() {   
  34.         return price;   
  35.     }   
  36.     public void setPrice(Double price) {   
  37.         this.price = price;   
  38.     }   
  39.     public Date getPrintTime() {   
  40.         return printTime;   
  41.     }   
  42.     public void setPrintTime(Date printTime) {   
  43.         this.printTime = printTime;   
  44.     }   
  45. }  

2、创建页面类BookList,代码如下:

java 代码
  1. import java.util.ArrayList;   
  2. import java.util.Date;   
  3. import java.util.List;   
  4.   
  5. import org.apache.tapestry.Block;   
  6. import org.apache.tapestry.ComponentResources;   
  7. import org.apache.tapestry.annotations.Retain;   
  8. import org.apache.tapestry.beaneditor.BeanModel;   
  9. import org.apache.tapestry.ioc.annotations.Inject;   
  10. import org.apache.tapestry.services.BeanModelSource;   
  11.   
  12. import com.example.entities.Book;   
  13.   
  14. public class BookList {   
  15.     @Inject  
  16.     private BeanModelSource _modelSource;   
  17.   
  18.     @Inject  
  19.     private ComponentResources _resources;   
  20.   
  21.     @Retain  
  22.     private BeanModel _model;   
  23.   
  24.     @Inject  
  25.     private Block _noData;   
  26.   
  27.     private Book book;   
  28.   
  29.     public Book getBook() {   
  30.         return book;   
  31.     }   
  32.   
  33.     public void setBook(Book book) {   
  34.         this.book = book;   
  35.     }   
  36.   
  37.     public BeanModel getModel() {   
  38.         return _model;   
  39.     }   
  40.   
  41.     public void setModel(BeanModel model) {   
  42.         _model = model;   
  43.     }   
  44.   
  45.     public BeanModelSource getModelSource() {   
  46.         return _modelSource;   
  47.     }   
  48.   
  49.     public void setModelSource(BeanModelSource modelSource) {   
  50.         _modelSource = modelSource;   
  51.     }   
  52.   
  53.     public ComponentResources getResources() {   
  54.         return _resources;   
  55.     }   
  56.   
  57.     public void setResources(ComponentResources resources) {   
  58.         _resources = resources;   
  59.     }   
  60.   
  61.     public Block getNoData() {   
  62.         return _noData;   
  63.     }   
  64.   
  65.     public void setNoData(Block noData) {   
  66.         _noData = noData;   
  67.     }   
  68.   
  69.     /**  
  70.      * 构造页面显示的model  
  71.      *   
  72.      * @return  
  73.      */  
  74.     public BeanModel getModelForGrid() {   
  75.         // create方法描述请查看接口描述   
  76.         _model = _modelSource.create(Book.classtrue, _resources);   
  77.         _model.remove("beDeleted");// 删除该列,在页面将不被显示   
  78.         return _model;   
  79.     }   
  80.   
  81.     /**  
  82.      * 页面调用,显示图书列表  
  83.      *   
  84.      * @return  
  85.      */  
  86.     public List getBookList() {   
  87.         return bookList();   
  88.     }   
  89.   
  90.     /**  
  91.      * 循环构造图书列表,模拟数据库取出的数据 其中list的深度可以是大于1层的  
  92.      *   
  93.      * @return  
  94.      */  
  95.     private List bookList() {   
  96.         List lst = new ArrayList(100);   
  97.         for (int i = 0; i < 100; i++) {   
  98.             Book b = new Book();   
  99.             b.setAuthor("HongQi");   
  100.             b.setBeDeleted("delete");   
  101.             b.setBookConcern("国务院");// 若页面不能显示中文,将此处换成E文   
  102.             b.setBookName("《不曾屈服的小草" + i + "》");// 若页面不能显示中文,将此处换成E文   
  103.             b.setPrice(15.55 + i);   
  104.             b.setPrintTime(new Date());   
  105.             lst.add(b);   
  106.         }   
  107.         return lst;   
  108.     }   
  109. }  

 

3、创建页面文件BookList.tml,代码如下:

xml 代码
  1. <html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">  
  2. <head>      
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />      
  4. <title>Samples - Book List</title>      
  5. </head>  
  6. <body>      
  7. <t:block id="nodata">没有找到数据!</t:block>      
  8. <h1>List All Books</h1>      
  9. <table t:type="Grid" model="modelForGrid" source="booklist"      
  10.     row="book"  rowsPerPage="10" empty="nodata">       
  11. </table>  
  12. </body>  
  13. </html>  

 

启动服务,访问创建好的页面,会看到你高兴的结果

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值