如何编写分页逻辑?

任何人都可以提供一些想法/逻辑来为我正在研究的搜索页面编写分页逻辑吗? 我所拥有的信息是该搜索的总页数 – 每页 10条记录我也被发送了前一页和下一页的编号(没有问题写下我需要做的逻辑我拉这些信息并填充。我是还获取我所在页面的信息。我只能显示10页,如下所示

 

假设总页数为15,当用户点击下一步时,我需要显示如下

  

在任何时候我只需要在分页中显示10页。

  #set($start = 1) #set($end = $Integer.parseInt($searchTO.getPagination().getNumberofPages())) #set($range = [$start..$end]) #set($iter = 1) #foreach($i in $range) #foreach($link in $searchTO.getPagination().getDirectPageLinks()) #if($i == $iter) #if ($Integer.parseInt($searchTO.getPagination().getPageNumber())==$iter) $i &nbsp| #else $i &nbsp| #end #set($iter = 1) #break #else #set($iter=$iter+1) #end #end #end 

以下是我将如何实现它:创建一个过滤数据并包含与分页相关的信息的Filter类通常是个好主意。 我使用这样的东西:

 public abstract class Filter{ /** Member identifier for the current page number */ private int currentPageNo; /** Member identifier for the current start page number in the page navigation */ private int currentStartPageNo; /** Member identifier for the current end page number in the page navigation */ private int currentEndPageNo; /** Member identifier for the number of elements on a page */ private int elementsPerPage; /** Member identifier for the number of pages you have in the navigation (ie 2 to 11 or 3 to 12 etc.) */ private int pageNumberInNavigation; public abstract Query createCountQuery(); public abstract Query createQuery(); public void setCurrentPageNo(){ //Your code here //Validation, variable setup } public Long getAllElementsCount(){ //Now this depends on the presistence framework you use, this code is //just for guidance and has Hibernate-like syntax Query query = createCountQuery(); List list = query.list(); return !list.isEmpty() && list.get(0) != null ? query.list().get(0) : 0; } public List getElements(){ //Now this depends on the presistence framework you use, this code is //just for guidance and has Hibernate-like syntax Query query = createQuery(); int from = ((currentPageNo - 1) * elementsPerPage); query.setFirstResult(from); query.setMaxResults(elementsPerPage); //If you use Hibernate, you don't need to worry for null check since if there are no results then an empty collection is returned return query.list(); } public List getAllElements(){ Query query = createQuery(); return query.list(); } public void refresh(){ //Your code here } public List next(){ //Move to the next page if exists setCurrentPageNo(getCurrentPageNo() + 1); getElements(); } public List previoius(){ //Move to the previous page if exists setCurrentPageNo(getCurrentPageNo() - 1); getElements(); } } 

你可以有特殊的过滤子类(取决于你想要检索的内容),每个子类都会实现它的createCountQuery()createQuery()

然后,您可以将Filter放到Velocity上下文中,然后您可以从此类中检索所需的所有信息。

当您设置当前页面时,您将更新所需的所有其他信息(即currentStartPageNo,currentEndPageNo)。

您还可以使用refresh()方法将filter恢复到其初始状态。

当然,当用户在Filter所属的搜索页面上导航时,您应该在会话中保留相同filter的实例(我的意思是您的Web框架,如Struts,Turbine等)。

这只是一个指导方针,一个想法,它不是完全编写的可执行代码,只是一个让你开始朝着一个方向前进的例子。

我还建议你一个名为jqGrid的jQuery插件,它有分页支持(虽然你必须有一个支持检索数据)和更多很酷的东西。 您可以使用它在网格中显示数据。 我和Velocity一起使用它没问题。 它有很多非常好用和有用的function,如filter工具栏,可编辑单元格, JSONXML等数据传输。老实说,我不知道它是否有你需要的分页(我用它只有一个页面显示在导航中并且你不能点击一个页面只需使用下一个上一个按钮进行导航),但它可能有支持。

服务器端分页 为了在一个页面中显示所有数据,拆分成部分以吸引用户注意。 我正在使用display-tag (要使用它在POJO类中维护一个字段来查看所有记录数据)

  

Employee List

Edit Delete
<%@taglib uri="http://displaytag.sf.net" prefix="display" %>

请参阅此现有问题: Java中的分页

使用JPanel的另一个分页,根据您的代码修改它。

对于所有语言,分页概念保持相同,并且代码实现需要基于语言的一点修改。

Java分页

有关详细信息,请参阅另一个现有问题:

java中的分页?

其他外部网站url: –

  • 在Web应用程序中实现搜索结果分页
  • 使用Spring的分页技术
  • 分页:服务器端还是客户端?