⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 在jsf中实现分页 jsf- 实现 java● 咖啡馆 -- jsf中文论坛.htm

📁 jsf分页处理, 很 好的例子, 可以很方便的移植
💻 HTM
📖 第 1 页 / 共 5 页
字号:
              <TBODY>
              <TR>
                <TD align=middle><BR><IMG 
                  src="在JSF中实现分页 JSF- 实现 Java● 咖啡馆 -- JSF中文论坛.files/0.gif" 
                  border=0></TD></TR></TBODY></TABLE><BR><IMG 
            src="在JSF中实现分页 JSF- 实现 Java● 咖啡馆 -- JSF中文论坛.files/10super.gif"><BR>级别: 
            <FONT color=#555555>论坛管理员<IMG alt=该用户目前不在线 
            src="在JSF中实现分页 JSF- 实现 Java● 咖啡馆 -- JSF中文论坛.files/offonline.gif"></FONT><BR>发贴: 
            <FONT color=green><SPAN class=bold>618</SPAN></FONT> (精华:<FONT 
            color=green><SPAN class=bold>0</SPAN></FONT>)<BR>威望: <FONT 
            color=#984b98><SPAN class=bold>268</SPAN></FONT><BR>咖啡豆: <FONT 
            color=red><SPAN 
            class=bold>2054</SPAN></FONT><BR>注册时间:2006-06-01<BR>最后登陆:2007-03-24 
          </TD>
          <TD vAlign=top width="80%" bgColor=#f7f7f7 height="100%">
            <TABLE style="TABLE-LAYOUT: fixed; WORD-WRAP: break-word" 
            height="100%" cellSpacing=0 cellPadding=4 width="99%" 
              align=center><TBODY>
              <TR>
                <TD vAlign=top width="91%" bgColor=#f7f7f7 colSpan=5><A 
                  href="http://www.hexiao.cn/jsf/profile.php?action=show&amp;username=icess"><IMG 
                  alt=查看作者资料 
                  src="在JSF中实现分页 JSF- 实现 Java● 咖啡馆 -- JSF中文论坛.files/profile.gif" 
                  align=absMiddle border=0></A> <A 
                  href="http://www.hexiao.cn/jsf/message.php?action=write&amp;msgid=icess"><IMG 
                  alt=发送短消息 
                  src="在JSF中实现分页 JSF- 实现 Java● 咖啡馆 -- JSF中文论坛.files/message.gif" 
                  align=absMiddle border=0></A> <A title=发送邮件 
                  href="http://www.hexiao.cn/jsf/sendemail.php?username=icess"><IMG 
                  src="在JSF中实现分页 JSF- 实现 Java● 咖啡馆 -- JSF中文论坛.files/email.gif" 
                  align=absMiddle border=0></A> <A 
                  href="http://www.hexiao.cn/jsf/sendemail.php?action=tofriend&amp;fid=13&amp;tid=13&amp;fpage="><IMG 
                  alt=推荐此贴 
                  src="在JSF中实现分页 JSF- 实现 Java● 咖啡馆 -- JSF中文论坛.files/emailto.gif" 
                  align=absMiddle border=0></A> <A 
                  href="http://www.hexiao.cn/jsf/post.php?action=quote&amp;fid=13&amp;tid=13&amp;article=1"><IMG 
                  alt=引用回复这个贴子 
                  src="在JSF中实现分页 JSF- 实现 Java● 咖啡馆 -- JSF中文论坛.files/quote.gif" 
                  align=absMiddle border=0></A> <A 
                  href="http://www.hexiao.cn/jsf/post.php?action=modify&amp;fid=13&amp;tid=13&amp;article=1"><IMG 
                  alt=删除或编辑改帖子 
                  src="在JSF中实现分页 JSF- 实现 Java● 咖啡馆 -- JSF中文论坛.files/edit.gif" 
                  align=absMiddle border=0></A> 【字体:<A 
                  onclick="text1.style.fontSize='16px';" 
                  href="javascript:;">大</A> <A 
                  onclick="text1.style.fontSize='14px';" 
                  href="javascript:;">中</A> <A 
                  onclick="text1.style.fontSize='12px';" 
                  href="javascript:;">小</A>】 
                  <HR width="100%" color=#e5e3e3 SIZE=1>
                  <IMG src="在JSF中实现分页 JSF- 实现 Java● 咖啡馆 -- JSF中文论坛.files/13.gif" 
                  align=left border=0> <SPAN 
                  class=tpc_title>&nbsp;</SPAN><BR><SPAN class=tpc_content 
                  id=text1><BR><BR>在JSF中实现<B 
                  style="COLOR: red; BACKGROUND-COLOR: #ffff66">分页</B></FONT>(二)<BR><BR><BR>&nbsp; 
                  前面一篇直接使用了Myfaces中的两个Component完成了一个简单的<B 
                  style="COLOR: red; BACKGROUND-COLOR: #ffff66">分页</B></FONT>,这里将会介绍一种On-demand 
                  loading的方法来进行<B 
                  style="COLOR: red; BACKGROUND-COLOR: #ffff66">分页</B></FONT>,仅仅在需要数据的时候加载。<BR><BR>&nbsp; 
                  先来说一些题外话,为了实现这种方式的<B 
                  style="COLOR: red; BACKGROUND-COLOR: #ffff66">分页</B></FONT>,公司里大约5-6个人做了半个多月的工作,扩展了dataTable,修改了dataScrollor,以及各种其他的方法,但是都不是很优雅。在上个月底的时候,在Myfaces的Mail 
                  List中也针对这个问题展开了一系列的讨论,最后有人总结了讨论中提出的比较好的方法,提出了以下的<B 
                  style="COLOR: red; BACKGROUND-COLOR: #ffff66">分页</B></FONT>方法,也是目前实现的最为优雅的方法,也就是不对dataTable和dataScrollor做任何修改,仅仅通过扩展DataModel来实现<B 
                  style="COLOR: red; BACKGROUND-COLOR: #ffff66">分页</B></FONT>。<BR><BR>&nbsp; 
                  DataModel是一个抽象类,用于封装各种类型的数据源和数据对象的访问,JSF中dataTable中绑定的数据实际上被包装成了一个DataModel,以消除各种不同数据源和数据类型的复杂性,在前面一篇中我们访问数据库并拿到了一个List,交给dataTable,这时候,JSF会将这个List包装成ListDataModel,dataTable访问数据都是通过这个DataModel进行的,而不是直接使用List。<BR><BR>&nbsp; 
                  接下来我们要将需要的页的数据封装到一个DataPage中去,这个类表示了我们需要的一页的数据,里面包含有三个元素:datasetSize,startRow,和一个用于表示具体数据的List。datasetSize表示了这个记录集的总条数,查询数据的时候,使用同样的条件取count即可,startRow表示该页的起始行在数据库中所有记录集中的位置。<BR><BR><BR><BR><FONT 
                  color=red>以下是代码:</FONT><BR><TEXTAREA class=java name=code rows=15 cols=100>/**
 * A simple class that represents a "page" of data out of a longer set, ie a
 * list of objects together with info to indicate the starting row and the full
 * size of the dataset. EJBs can return instances of this type when returning
 * subsets of available data.
 */
public class DataPage
{
 &nbsp;  private int datasetSize;
 &nbsp;  private int startRow;
 &nbsp;  private List data;

 &nbsp;  /** *//**
 &nbsp;   * Create an object representing a sublist of a dataset.
 &nbsp;   * 
 &nbsp;   * @param datasetSize
 &nbsp;   * &nbsp;  &nbsp;  &nbsp;  &nbsp; is the total number of matching rows available.
 &nbsp;   * 
 &nbsp;   * @param startRow
 &nbsp;   * &nbsp;  &nbsp;  &nbsp;  &nbsp; is the index within the complete dataset of the first element
 &nbsp;   * &nbsp;  &nbsp;  &nbsp;  &nbsp; in the data list.
 &nbsp;   * 
 &nbsp;   * @param data
 &nbsp;   * &nbsp;  &nbsp;  &nbsp;  &nbsp; is a list of consecutive objects from the dataset.
 &nbsp;   */
 &nbsp;  public DataPage(int datasetSize, int startRow, List data)
 &nbsp;  {
 &nbsp;  &nbsp;   this.datasetSize = datasetSize;
 &nbsp;  &nbsp;   this.startRow = startRow;
 &nbsp;  &nbsp;   this.data = data;
 &nbsp;  }

 &nbsp;  /** *//**
 &nbsp;   * Return the number of items in the full dataset.
 &nbsp;   */
 &nbsp;  public int getDatasetSize()
 &nbsp;  {
 &nbsp;  &nbsp;   return datasetSize;
 &nbsp;  }

 &nbsp;  /** *//**
 &nbsp;   * Return the offset within the full dataset of the first element in the
 &nbsp;   * list held by this object.
 &nbsp;   */
 &nbsp;  public int getStartRow()
 &nbsp;  {
 &nbsp;  &nbsp;   return startRow;
 &nbsp;  }

 &nbsp;  /** *//**
 &nbsp;   * Return the list of objects held by this object, which is a continuous
 &nbsp;   * subset of the full dataset.
 &nbsp;   */
 &nbsp;  public List getData()
 &nbsp;  {
 &nbsp;  &nbsp;   return data;
 &nbsp;  }
}
</TEXTAREA><BR><BR>接下来,我们要对DataModel进行封装,达到我们<B 
                  style="COLOR: red; BACKGROUND-COLOR: #ffff66">分页</B></FONT>的要求。该DataModel仅仅持有了一页的数据DataPage,并在适当的时候加载数据,读取我们需要页的数据。<BR><BR><FONT 
                  color=red>以下是代码:</FONT><BR><TEXTAREA class=java name=code rows=15 cols=100>/**
 * A special type of JSF DataModel to allow a datatable and datascroller to page
 * through a large set of data without having to hold the entire set of data in
 * memory at once.
 * &lt;p&gt;
 * Any time a managed bean wants to avoid holding an entire dataset, the managed
 * bean should declare an inner class which extends this class and implements
 * the fetchData method. This method is called as needed when the table requires
 * data that isn't available in the current data page held by this object.
 * &lt;p&gt;
 * This does require the managed bean (and in general the business method that
 * the managed bean uses) to provide the data wrapped in a DataPage object that
 * provides info on the full size of the dataset.
 */
public abstract class PagedListDataModel extends DataModel
{
 &nbsp;  int pageSize;
 &nbsp;  int rowIndex;
 &nbsp;  DataPage page;

 &nbsp;  /** *//**
 &nbsp;   * Create a datamodel that pages through the data showing the specified
 &nbsp;   * number of rows on each page.
 &nbsp;   */
 &nbsp;  public PagedListDataModel(int pageSize)
 &nbsp;  {
 &nbsp;  &nbsp;   super();
 &nbsp;  &nbsp;   this.pageSize = pageSize;
 &nbsp;  &nbsp;   this.rowIndex = -1;
 &nbsp;  &nbsp;   this.page = null;
 &nbsp;  }

 &nbsp;  /** *//**
 &nbsp;   * Not used in this class; data is fetched via a callback to the fetchData
 &nbsp;   * method rather than by explicitly assigning a list.
 &nbsp;   */

 &nbsp;  public void setWrappedData(Object o)
 &nbsp;  {
 &nbsp;  &nbsp;   if(o instanceof DataPage)
 &nbsp;  &nbsp;   {
 &nbsp;  &nbsp;  &nbsp;  &nbsp; this.page = (DataPage) o;
 &nbsp;  &nbsp;   }
 &nbsp;  &nbsp;   else
 &nbsp;  &nbsp;   {
 &nbsp;  &nbsp;  &nbsp;  &nbsp; throw new UnsupportedOperationException("setWrappedData");
 &nbsp;  &nbsp;   }
 &nbsp;  }

 &nbsp;  public int getRowIndex()
 &nbsp;  {
 &nbsp;  &nbsp;   return rowIndex;
 &nbsp;  }

 &nbsp;  /** *//**
 &nbsp;   * Specify what the "current row" within the dataset is. Note that the
 &nbsp;   * UIData component will repeatedly call this method followed by getRowData
 &nbsp;   * to obtain the objects to render in the table.
 &nbsp;   */

 &nbsp;  public void setRowIndex(int index)
 &nbsp;  {
 &nbsp;  &nbsp;   rowIndex = index;
 &nbsp;  }

 &nbsp;  /** *//**
 &nbsp;   * Return the total number of rows of data available (not just the number of
 &nbsp;   * rows in the current page!).
 &nbsp;   */

 &nbsp;  public int getRowCount()
 &nbsp;  {
 &nbsp;  &nbsp;   return getPage().getDatasetSize();
 &nbsp;  }

 &nbsp;  /** *//**
 &nbsp;   * Return a DataPage object; if one is not currently available then fetch
 &nbsp;   * one. Note that this doesn't ensure that the datapage returned includes
 &nbsp;   * the current rowIndex row; see getRowData.
 &nbsp;   */
 &nbsp;  private DataPage getPage()
 &nbsp;  {
 &nbsp;  &nbsp;   if (page != null)
 &nbsp;  &nbsp;   {
 &nbsp;  &nbsp;  &nbsp;  &nbsp; return page;
 &nbsp;  &nbsp;   }

 &nbsp;  &nbsp;   int rowIndex = getRowIndex();
 &nbsp;  &nbsp;   int startRow = rowIndex;
 &nbsp;  &nbsp;   if (rowIndex == -1)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -