`
ruruhuang
  • 浏览: 189011 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Tapestry Table 组件的强大(分页查询时只fetch当前页的数据)。

阅读更多

Tapestry Table组件提供了分页的功能, 但是以前以为它只能是一次性的吧所有元素都fetch出来,然后把这些元素交给Table去处理。错了。 其实Tapetry的Table组件太强大了, 它已经把这个问题考虑了。只fetch当前页的数据。
 
以前做从数据库中查找数据  然后再分页时, 都是使用For 或者 Foreach组件,然后自己写一个PageNavigation组件(用于显示第几页 有多少页),还有ColumnSort组件。 其实Table已经把这些都写好。而且每次写分页时都非常麻烦。 要注意的东西太多了。
 
我花了 大概半天的时间看了Table组件的实现。 其实Table组件的核心是这些组件(Table FormTable是由下列这些组件组合而成的)
TableView              采集Table需要的信息, Source Column etc
TableColumns         显示Table  表头的信息
TableFormRows      象For组件一样list 每行的内容(用在Form中,可以提交数据)
TableRows             象For组件一样list 每行的内容(不用在Form中)
TableValues           应该被TableRows或TableFormRows包含, 每行的值
TablePages            显示Page Navigation
TableFormPages     显示Page Navigation(在Form里)
 
详细信息可以结合WorkBench的Table例子看Table FormTable的源码, 应该能对Table有个比较深入的理解。
 
下面说说怎么让Table fetch当前页面所需的数据。不知道大家有没有注意到TableView的source属性:
source
 
Object[]
Collection
Iterator
IBasicTableModel
 
in You must provide either both source and columns parameters or the tableModel parameter   The data to be displayed by the component. This parameter must be used in combination with the columns parameter. The parameter must be an array of values, a collection, an iterator, or an object implementing the IBasicTableModel interface.
 
我们一般都是使用Object[] Collection 和 Iterator作为table的source, 有没有注意IBasicTableModel呢? 我们先看看这个接口。
 
package org.apache.tapestry.contrib.table.model;
import java.util.Iterator;
/**
 * A simplified version of the table model that concerns itself only with
 * providing the data on the current page.
 *
 * @author mindbridge
 * @since 3.0
 */
public interface IBasicTableModel
{
    /**
     *  Returns the number of all records
     *  @return the number of all rows
     **/
    int getRowCount();
    /**
     *  Returns the rows on the current page.
     *  @param nFirst the index of the first item to be dispayed
     *  @param nPageSize the number of items to be displayed
     *  @param objSortColumn the column to sort by or null if there is no sorting
     *  @param bSortOrder determines the sorting order (ascending or descending)
     **/
    Iterator getCurrentPageRows(int nFirst, int nPageSize, ITableColumn objSortColumn, boolean bSortOrder);
}
 
是不是有些清晰了呢? 两个方法, 没错就是上面javadoc上面说的那样。 我们返回一个实现了IBasicTableModel的类就能做到Fetch当前页的数据: 我做了一个小例子基本包括了Table的用法。我也是仿照WorkBench的Table的例子,但是我用的是Fetch当前页面的Locale,还写了一个自定义Column。 如下(注意加粗的部分):
 
TablePage.html
<html jwcid="$content$">
<body jwcid="&lt;/font&gt;&lt;a href=" mailto:border@border"="">border@Border" subTitle="Tapestry">


 
 
           source="ognl:model"
        columns="literal: !locale:toString(), !language:displayLanguage, !country:displayCountry,
               !variant:displayVariant, !isoLanguage:ISO3Language, !isoCountry:ISO3Country, =displayNameColumn"
               pageSize="ognl:7"
   >
   
    
     
           
           rows@contrib:TableRows" row="ognl:currLocale">
           
           
          
           
           
          
   

          

          

  

 
 



 
 
 

 


</body>
</html>
 
TablePage.java
 
package com.dengyin.tapestry.pages;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import org.apache.tapestry.annotations.InitialValue;
import org.apache.tapestry.annotations.Meta;
import org.apache.tapestry.annotations.Persist;
import org.apache.tapestry.contrib.table.model.IBasicTableModel;
import org.apache.tapestry.contrib.table.model.ITableColumn;
import org.apache.tapestry.contrib.table.model.simple.ITableColumnEvaluator;
import org.apache.tapestry.contrib.table.model.simple.SimpleTableColumn;
import com.dengyin.tapestry.DemoBasePage;
@Meta(
  { "anonymous-access=true", "admin-page=false" })
public abstract class TablePage extends DemoBasePage {
 
 @Persist("session")
 @InitialValue("new java.util.HashSet()")
 public abstract Set<locale> getSelectedLocales();
 public abstract void setSelectedLocales(Set<locale> set);
 
 publicabstract Locale getCurrLocale();
 @InitialValue("new java.util.HashSet()")
 public abstract Set<locale> getListItems();
 public abstract void setListItems(Set<locale> set);
 
 public boolean getCheckboxSelected(){
  return getSelectedLocales().contains(getCurrLocale());
 }
 
 public void setCheckboxSelected(boolean checked){
  Set<locale> selectedLocales = getSelectedLocales();
  if (checked){
   selectedLocales.add(getCurrLocale());
  }else{
   selectedLocales.remove(getCurrLocale());
  }
 
  //make persist
  setSelectedLocales(selectedLocales);
 }
 
 public ITableColumn getDisplayNameColumn(){
  return new SimpleTableColumn("displayName", "displayName", new ITableColumnEvaluator(){
   private static final long serialVersionUID = 6228368700745851970L;
   public Object getColumnValue(ITableColumn objColumn, Object objRow) {
    Locale locale = (Locale) objRow;
    return locale.getDisplayName();
   }
  
  }, false
   
  );
 }
 
 public IBasicTableModel getModel(){
  return new IBasicTableModel(){
   public int getRowCount() {
    return Locale.getAvailableLocales().length;
   }
   public Iterator getCurrentPageRows(int nFirst, int nPageSize, ITableColumn objSortColumn, boolean bSortOrder) {
    int count = Locale.getAvailableLocales().length;
    int begInIndex = nFirst; //nFirst the index of the element. not the page index
    int endIndex = nFirst + nPageSize -1;
    //if the last page don&apost contain pageSize&aposs elements, it will throw ArrayIndexOutOfBoundsException.
    // so we need to catch this.
    if (endIndex > count -1){
     endIndex = count -1;
   }
    List<locale> result = new ArrayList<locale>();
    for(int i = begInIndex; i<=endIndex; i++ ){
     result.add(Locale.getAvailableLocales()[i]);
    }
   
    return result.iterator();
   }
  
  };

 }
 public abstract Locale getItem();
 
 public void listItems(){
  setListItems(getSelectedLocales());
 }
}

getCurrentPageRows 方法中我们可以通过ITableColumn objSortColumn 去进行排序(当有需要排序时这个参数不为null), bSortOrder 为是升序还是降序排列。我们可以通过objSortColumn得到需要排序的字段, bSortOrder 是按种方式排序, 我们就可以构造SQL语句,把排序的工作交给数据库, 然后得到一个排好序的List, 然后在截取这个List中当前页的值就行了。

分享到:
评论

相关推荐

    tapestry table

    一个关于table在tapestry里应用的例子

    tapestry页面编辑组件

    tapestry页面编辑组件,可以实现文本框,单选框,多选框和下拉框等的自动生成,并返回改变后的数据。

    tapestry5 自定义组件

    自定义的邮件组件!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    Tapestry的组件及功能

    介绍Tapestry组件的使用和功能。内容还行,使用初学者入门。

    tapestry组件

    tapestry部分组件绑定参数的列表!

    tapestry4.02中封装ext的GridPanel组件

    tapestry4.02中封装ext的GridPanel组件

    Tapestry中的Table和Tree的完整教程

    Tapestry中的Table和Tree的完整教程

    tapestry hibernate Spring应用及组件的使用

    tapestry hibernate Spring应用及组件的使用的一个简单完整例子,包含form组件、table组件的一个增删改查。其中数据库创建用户和表在docs里,数据库使用oracle

    tapestry 5.1.0.5 官方组件文档 天涯浪子

    来自:http://tapestry.apache.org/tapestry5.1/tapestry-core/ref

    tapestry5中文文档

    tapestry5组件说明使用及登陆修改等简单实例

    tapestry教程资料文档合集

    Tapestry5最新中文教程.doc 作者 Renat Zubairov & Igor Drobiazko译者 沙晓兰 发布于 2008年7月2日 下午9时30分 社区 Java 主题 Web框架 ----------------------------------------- Tapestry5.1实例教程.pdf ...

    tapestry技术

    Tapestry是一个开源的基于servlet的应用程序框架,它使用组件对象模型来创建动态的,交互的web应用。一个组件就是任意一个带有jwcid属性的html标记。其中jwc的意思是Java Web Component。Tapestry使得java代码与html...

    tapestry5以上的帮助事例,帮助文档与spring衔接文档

    你用Tapestry开发Web应用时你无需关注以操作为中心的(Operation-centric) Servlet API.引用Tapestry网站上的一句话:"Tapestry用对象(objects),方法(methods),属性(PRoperties)替代以往的URLs和查询参数, 重新...

    Tapestry开发指南

    Tapestry是一个开源的基于servlet的应用程序框架,它使用组件对象模型来创建动态的,交互的web应用。一个组件就是任意一个带有jwcid属性的html标记。其中jwc的意思是Java Web Component。Tapestry使得java代码与html...

    Tapestry通用WEB框架

    Tapestry通用WEB框架支持,切换皮肤,自主分页,

    Tapestry简单入门.rar_java Tapestry_tapestry

    Tapestry简单入门介绍,包含Tapestry入门、tapestry组件介绍

    Tapestry5最新中文入门实例教程

    本文介绍Tapestry框架版本5。本文利用Tapestry 5开发一个简单的具有创建/读/更新/删除功能的应用,在创建这个应用的过程中,本文体会到Tapestry...还将了解如何应用Tapestry中内嵌的Ajax功能来创建支持Ajax的组件。

    Maven + Tapestry5.3.8 + Spring4.0.5 + Oracle10g

    这是Tapestry5.3.8 版本的一个大Demo,集合Spring4.0, 采用Maven 项目管理工具,没有集合Hibernate。 之所以说是个大Demo,是因为这项目中包含的内容并不少,包含: 1)解决了Tapestry5.3.8中文Bug问题 2)Tapestry...

    深入浅出Tapestry

    资源名称:深入浅出Tapestry内容简介:本书以循序渐进的方式,从Tapestry框架技术的基本概念入手,讲解Tapestry框架在J2EE Web应用程序中的整体架构实现。使读者在学习如何使用Tapestry框架技术的同时,还能够获得在...

    tapestry 实例

    tapestry 实例tapestry 实例tapestry 实例tapestry 实例

Global site tag (gtag.js) - Google Analytics