java – 如何编写分页逻辑?

前端之家收集整理的这篇文章主要介绍了java – 如何编写分页逻辑?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
任何人都可以提供一些想法/逻辑来为我正在研究的搜索页面编写分页逻辑吗?
我所拥有的信息是该搜索的总页数 – 每页10条记录我也被发送了前一页和下一页的编号(没有问题写我需要做的逻辑我拉这些信息并填充.我是也获取我所在页面的信息.我只能显示10页,如下所示
  1. <prevIoUs 1 |2 |3 | 4| 5 | 6 | 7 | 8 | 9 | 10 next>

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

  1. <prevIoUs 2 |3 |4 |5 |6 |7 |8 |9 |10 |11 next>

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

  1. #set($start = 1)
  2. #set($end = $Integer.parseInt($searchTO.getPagination().getNumberofPages()))
  3. #set($range = [$start..$end])
  4.  
  5. #set($iter = 1)
  6. #foreach($i in $range)
  7. #foreach($link in $searchTO.getPagination().getDirectPageLinks())
  8. #if($i == $iter)
  9. #if ($Integer.parseInt($searchTO.getPagination().getPageNumber())==$iter)
  10. <a class="search_current" href="/?_page=SEARCH&_action=SEARCH$link">$i &nbsp|</a>
  11. #else
  12. <a href="/?_page=SEARCH&_action=SEARCH$link">$i &nbsp|</a>
  13. #end
  14. #set($iter = 1)
  15. #break
  16. #else
  17. #set($iter=$iter+1)
  18. #end
  19.  
  20. #end
  21.  
  22. #end

解决方法

以下是我将如何实现它:
通常,最好创建一个Filter类来过滤数据并为您包含与分页相关的信息.我使用这样的东西:
  1. public abstract class Filter{
  2.  
  3. /** Member identifier for the current page number */
  4. private int currentPageNo;
  5.  
  6. /** Member identifier for the current start page number in the page navigation */
  7. private int currentStartPageNo;
  8.  
  9. /** Member identifier for the current end page number in the page navigation */
  10. private int currentEndPageNo;
  11.  
  12. /** Member identifier for the number of elements on a page */
  13. private int elementsPerPage;
  14.  
  15. /** Member identifier for the number of pages you have in the navigation (i.e 2 to 11 or 3 to 12 etc.) */
  16. private int pageNumberInNavigation;
  17.  
  18. public abstract Query createCountQuery();
  19.  
  20. public abstract Query createQuery();
  21.  
  22. public void setCurrentPageNo(){
  23. //Your code here
  24. //Validation,variable setup
  25. }
  26.  
  27. public Long getAllElementsCount(){
  28. //Now this depends on the presistence framework you use,this code is
  29. //just for guidance and has Hibernate-like Syntax
  30. Query query = createCountQuery();
  31. List list = query.list();
  32. return !list.isEmpty() && list.get(0) != null ? query.list().get(0) : 0;
  33. }
  34.  
  35. public List getElements(){
  36. //Now this depends on the presistence framework you use,this code is
  37. //just for guidance and has Hibernate-like Syntax
  38. Query query = createQuery();
  39. int from = ((currentPageNo - 1) * elementsPerPage);
  40. query.setFirstResult(from);
  41. query.setMaxResults(elementsPerPage);
  42. //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
  43. return query.list();
  44. }
  45.  
  46. public List getAllElements(){
  47. Query query = createQuery();
  48. return query.list();
  49. }
  50.  
  51. public void refresh(){
  52. //Your code here
  53. }
  54.  
  55. public List next(){
  56. //Move to the next page if exists
  57. setCurrentPageNo(getCurrentPageNo() + 1);
  58. getElements();
  59. }
  60.  
  61. public List previoius(){
  62. //Move to the prevIoUs page if exists
  63. setCurrentPageNo(getCurrentPageNo() - 1);
  64. getElements();
  65. }
  66.  
  67. }

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

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

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

您还可以使用refresh()方法将过滤器恢复到其初始状态.

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

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

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

猜你在找的Java相关文章