1、首先在实体包下建立实体类Book,代码如下:
java 代码
- public class Book {
- private String bookName;
- private String author;
- private Double price;
- private String bookConcern;
- private Date printTime;
- private String beDeleted;
- public String getBeDeleted() {
- return beDeleted;
- }
- public void setBeDeleted(String beDeleted) {
- this.beDeleted = beDeleted;
- }
- public String getAuthor() {
- return author;
- }
- public void setAuthor(String author) {
- this.author = author;
- }
- public String getBookConcern() {
- return bookConcern;
- }
- public void setBookConcern(String bookConcern) {
- this.bookConcern = bookConcern;
- }
- public String getBookName() {
- return bookName;
- }
- public void setBookName(String bookName) {
- this.bookName = bookName;
- }
- public Double getPrice() {
- return price;
- }
- public void setPrice(Double price) {
- this.price = price;
- }
- public Date getPrintTime() {
- return printTime;
- }
- public void setPrintTime(Date printTime) {
- this.printTime = printTime;
- }
- }
2、创建页面类BookList,代码如下:
java 代码
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
- import org.apache.tapestry.Block;
- import org.apache.tapestry.ComponentResources;
- import org.apache.tapestry.annotations.Retain;
- import org.apache.tapestry.beaneditor.BeanModel;
- import org.apache.tapestry.ioc.annotations.Inject;
- import org.apache.tapestry.services.BeanModelSource;
- import com.example.entities.Book;
- public class BookList {
- @Inject
- private BeanModelSource _modelSource;
- @Inject
- private ComponentResources _resources;
- @Retain
- private BeanModel _model;
- @Inject
- private Block _noData;
- private Book book;
- public Book getBook() {
- return book;
- }
- public void setBook(Book book) {
- this.book = book;
- }
- public BeanModel getModel() {
- return _model;
- }
- public void setModel(BeanModel model) {
- _model = model;
- }
- public BeanModelSource getModelSource() {
- return _modelSource;
- }
- public void setModelSource(BeanModelSource modelSource) {
- _modelSource = modelSource;
- }
- public ComponentResources getResources() {
- return _resources;
- }
- public void setResources(ComponentResources resources) {
- _resources = resources;
- }
- public Block getNoData() {
- return _noData;
- }
- public void setNoData(Block noData) {
- _noData = noData;
- }
- /**
- * 构造页面显示的model
- *
- * @return
- */
- public BeanModel getModelForGrid() {
- // create方法描述请查看接口描述
- _model = _modelSource.create(Book.class, true, _resources);
- _model.remove("beDeleted");// 删除该列,在页面将不被显示
- return _model;
- }
- /**
- * 页面调用,显示图书列表
- *
- * @return
- */
- public List getBookList() {
- return bookList();
- }
- /**
- * 循环构造图书列表,模拟数据库取出的数据 其中list的深度可以是大于1层的
- *
- * @return
- */
- private List bookList() {
- List lst = new ArrayList(100);
- for (int i = 0; i < 100; i++) {
- Book b = new Book();
- b.setAuthor("HongQi");
- b.setBeDeleted("delete");
- b.setBookConcern("国务院");// 若页面不能显示中文,将此处换成E文
- b.setBookName("《不曾屈服的小草" + i + "》");// 若页面不能显示中文,将此处换成E文
- b.setPrice(15.55 + i);
- b.setPrintTime(new Date());
- lst.add(b);
- }
- return lst;
- }
- }
3、创建页面文件BookList.tml,代码如下:
xml 代码
- <html 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 - Book List</title>
- </head>
- <body>
- <t:block id="nodata">没有找到数据!</t:block>
- <h1>List All Books</h1>
- <table t:type="Grid" model="modelForGrid" source="booklist"
- row="book" rowsPerPage="10" empty="nodata">
- </table>
- </body>
- </html>
启动服务,访问创建好的页面,会看到你高兴的结果