spring mvc 分页

spring mvc 分页
分页主要需要两个参数:

1、当前页是第几页

2、每页展示多少条数据

先写一个类来封装处理这两个参数:

[java]  view plain copy
  1. package com.smvc.annonation.utils;  
  2.   
  3. import java.io.Serializable;  
  4. import java.util.List;  
  5.   
  6. import org.apache.commons.lang.builder.ToStringBuilder;  
  7. import org.apache.commons.lang.builder.ToStringStyle;  
  8.   
  9. public class ResultFilter<T> implements Serializable{  
  10.   
  11.     private static final long serialVersionUID = 5472321653620726832L;  
  12.   
  13.     private final static int DEFAULT_NAVIGATOR_SIZE = 5;  
  14.   
  15.     //当前页  
  16.     private int currentPage = 1;  
  17.     //每页显示数量  
  18.     private int pageSize = 5;  
  19.       
  20.     //总条数  
  21.     private int totalCount;  
  22.   
  23.     private boolean havaNextPage;  
  24.   
  25.     private boolean havePrePage;  
  26.   
  27.     private int navigatorSize;  
  28.       
  29.     //存放查询结果用的list  
  30.     private List<T> items;  
  31.       
  32.     public ResultFilter(){  
  33.           
  34.     }  
  35.       
  36.     public ResultFilter(int totalCount, int pageSize, int currentPage) {  
  37.         this(totalCount, pageSize, currentPage, DEFAULT_NAVIGATOR_SIZE);  
  38.     }  
  39.   
  40.     public ResultFilter(int totalCount, int pageSize, int currentPage,  
  41.                         int navigatorSize) {  
  42.         this.totalCount = totalCount;  
  43.         this.pageSize = pageSize;  
  44.         this.currentPage = currentPage;  
  45.         this.navigatorSize = navigatorSize;  
  46.     }  
  47.       
  48.     public int getPageCount() {  
  49.         int pageCount = 0;  
  50.         if (pageSize != 0) {  
  51.             pageCount = totalCount / pageSize;  
  52.             if (totalCount % pageSize != 0)  
  53.                 pageCount++;  
  54.         }  
  55.   
  56.         return pageCount;  
  57.     }  
  58.   
  59.     public int getCurrentPage() {  
  60.         currentPage = currentPage < getPageCount() ? currentPage :  
  61.                       getPageCount();  
  62.         currentPage = currentPage < 1 ? 1 : currentPage;  
  63.   
  64.         return currentPage;  
  65.     }  
  66.   
  67.     public int getPageSize() {  
  68.         return pageSize;  
  69.     }  
  70.   
  71.     public int getTotalCount() {  
  72.         return totalCount;  
  73.     }  
  74.   
  75.   
  76.     public boolean isHaveNextPage() {  
  77.         havaNextPage = false;  
  78.         if ((getPageCount() > 1) && (getPageCount() > getCurrentPage()))  
  79.             havaNextPage = true;  
  80.         return havaNextPage;  
  81.     }  
  82.   
  83.     public boolean isHavePrePage() {  
  84.         havePrePage = false;  
  85.         if ((getPageCount() > 1) && (currentPage > 1))  
  86.             havePrePage = true;  
  87.         return havePrePage;  
  88.     }  
  89.   
  90.     private int getNavigatorIndex(boolean isBegin) {  
  91.         int beginNavigatorIndex = getCurrentPage() - navigatorSize / 2;  
  92.         int endNavigatorIndex = getCurrentPage() + navigatorSize / 2;  
  93.         beginNavigatorIndex = beginNavigatorIndex < 1 ? 1 : beginNavigatorIndex;  
  94.         endNavigatorIndex = endNavigatorIndex < getPageCount() ?  
  95.                             endNavigatorIndex :  
  96.                             getPageCount();  
  97.         while ((endNavigatorIndex - beginNavigatorIndex) < navigatorSize &&  
  98.                (beginNavigatorIndex != 1 || endNavigatorIndex != getPageCount())) {  
  99.             if (beginNavigatorIndex > 1)  
  100.                 beginNavigatorIndex--;  
  101.             else if (endNavigatorIndex < getPageCount())  
  102.                 endNavigatorIndex++;  
  103.         }  
  104.   
  105.         if(isBegin)  
  106.             return beginNavigatorIndex;  
  107.         else  
  108.             return endNavigatorIndex;  
  109.     }  
  110.   
  111.     public int getBeginNavigatorIndex() {  
  112.         return getNavigatorIndex(true);  
  113.     }  
  114.   
  115.     public int getEndNavigatorIndex() {  
  116.         return getNavigatorIndex(false);  
  117.     }  
  118.   
  119.     public List<T> getItems() {  
  120.         return items;  
  121.     }  
  122.   
  123.     public void setItems(List<T> items) {  
  124.         this.items = items;  
  125.     }  
  126.       
  127.     public void setCurrentPage(int currentPage) {  
  128.         this.currentPage = currentPage;  
  129.     }  
  130.   
  131.     public void setPageSize(int pageSize) {  
  132.         this.pageSize = pageSize;  
  133.     }  
  134.   
  135.     public void setTotalCount(int totalCount) {  
  136.         this.totalCount = totalCount;  
  137.     }  
  138.   
  139.     @Override  
  140.     public String toString() {  
  141.         return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);  
  142.     }  
  143.   
  144. }  

接着,就要处理如何翻页的问题。解决方法就是在页面间传递参数,传递当前是第几页。

使用自己定义标签实现比较简便(由于现在只做了一个页面,并不能保证翻页功能的通用性,以后会慢慢完善)

自定义标签的配置:

my.tld

[html]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <taglib xmlns="http://java.sun.com/xml/ns/j2ee"  
  3.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"  
  5.         version="2.0">  
  6.   
  7.     <description>My Tag Library</description>  
  8.     <tlib-version>1.0</tlib-version>  
  9.     <short-name>my</short-name>  
  10.     <uri>my-taglib</uri>  
  11.   
  12.     <tag>  
  13.         <description>split page</description>  
  14.         <name>paging</name>  
  15.         <tag-class>com.smvc.annonation.tag.PagingTag</tag-class>  
  16.         <body-content>empty</body-content>  
  17.         <attribute>  
  18.             <description>base href</description>  
  19.             <name>href</name>  
  20.             <required>false</required>  
  21.             <rtexprvalue>true</rtexprvalue>  
  22.         </attribute>  
  23.         <attribute>  
  24.             <description>curr page</description>  
  25.             <name>curr</name>  
  26.             <required>true</required>  
  27.             <rtexprvalue>true</rtexprvalue>  
  28.         </attribute>  
  29.         <attribute>  
  30.             <description>page size</description>  
  31.             <name>size</name>  
  32.             <required>true</required>  
  33.             <rtexprvalue>true</rtexprvalue>  
  34.         </attribute>  
  35.         <attribute>  
  36.             <description>total page</description>  
  37.             <name>total</name>  
  38.             <required>true</required>  
  39.             <rtexprvalue>true</rtexprvalue>  
  40.         </attribute>  
  41.         <attribute>  
  42.             <description>curr parameter name</description>  
  43.             <name>cparam</name>  
  44.             <required>false</required>  
  45.             <rtexprvalue>false</rtexprvalue>  
  46.         </attribute>  
  47.         <attribute>  
  48.             <description>page size parameter name</description>  
  49.             <name>sparam</name>  
  50.             <required>false</required>  
  51.             <rtexprvalue>false</rtexprvalue>  
  52.         </attribute>  
  53.         <dynamic-attributes>false</dynamic-attributes>  
  54.     </tag>  
  55.       
  56.       
  57.       
  58.     <tag>  
  59.         <description>front split page</description>  
  60.         <name>frontPaging</name>  
  61.         <tag-class>com.aspire.cms.demo.framework.tag.FrontPagingTag</tag-class>  
  62.         <body-content>empty</body-content>  
  63.         <attribute>  
  64.             <description>base href</description>  
  65.             <name>href</name>  
  66.             <required>false</required>  
  67.             <rtexprvalue>true</rtexprvalue>  
  68.         </attribute>  
  69.         <attribute>  
  70.             <description>result filter</description>  
  71.             <name>rf</name>  
  72.             <required>true</required>  
  73.             <rtexprvalue>true</rtexprvalue>  
  74.         </attribute>  
  75.         <dynamic-attributes>false</dynamic-attributes>  
  76.     </tag>  
  77.   
  78. </taglib>  

对应的java类:

[java]  view plain copy
  1. package com.smvc.annonation.tag;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import javax.servlet.jsp.JspException;  
  6. import javax.servlet.jsp.JspWriter;  
  7. import javax.servlet.jsp.tagext.SimpleTagSupport;  
  8.   
  9. import org.apache.commons.lang.StringUtils;  
  10.   
  11. public class PagingTag extends SimpleTagSupport {  
  12.       
  13.     private String href;  
  14.       
  15.     //当前页  
  16.     private String cparam;  
  17.     //每页条数  
  18.     private String sparam;  
  19.   
  20.     private int curr;//当前页  
  21.       
  22.     private int size;//每页条数  
  23.       
  24.     private int total;//总页数  
  25.       
  26.     @Override  
  27.     public void doTag() throws JspException, IOException {  
  28.         JspWriter out = getJspContext().getOut();  
  29.           
  30.         if(StringUtils.isEmpty(cparam)) {  
  31.             cparam = "currentPage";  
  32.         }  
  33.         if(StringUtils.isEmpty(sparam)) {  
  34.             sparam = "pageSize";  
  35.         }  
  36.           
  37.         if(!href.endsWith("?") && !href.endsWith("&")) {  
  38.             if(href.indexOf("?") == -1) {  
  39.                 href = href + "?";  
  40.             } else {  
  41.                 href = href + "&";  
  42.             }  
  43.         }  
  44.           
  45.         if (curr <= 0) {  
  46.             curr = 1;  
  47.         } else if (curr > total) {  
  48.             curr = total;  
  49.         }  
  50.           
  51.         out.append("<span>");  
  52.         // 首页  
  53.         if (curr == 1) {  
  54.             out.append("首页");  
  55.         } else {  
  56.             href(out, href, 1"首页");  
  57.         }  
  58.         out.append(" | ");  
  59.         // 上一页  
  60.         if (curr == 1) {  
  61.             out.append("上一页");  
  62.         } else {  
  63.             href(out, href, curr - 1"上一页");  
  64.         }  
  65.         out.append(" | ");  
  66.         // 下一页  
  67.         if (curr == total) {  
  68.             out.append("下一页");  
  69.         } else {  
  70.             href(out, href, curr + 1"下一页");  
  71.         }  
  72.         out.append(" | ");  
  73.         // 末页  
  74.         if (curr == total) {  
  75.             out.append("末页");  
  76.         } else {  
  77.             href(out, href, total, "末页");  
  78.         }  
  79.         out.append("</span>");  
  80.         out.append("<span>第");  
  81.         out.append(curr + "/" + total);  
  82.         out.append("页</span>");  
  83.           
  84.           
  85.         super.doTag();  
  86.     }  
  87.       
  88.     private void href(JspWriter out, String href, int curr, String title) throws IOException {  
  89.         out.append("<a href=\"").append(href).append(cparam).append("=").append("" + curr).append("&").append(sparam).append("=").append("" + size).append("\">").append(title).append("</a>");  
  90.     }  
  91.   
  92.     public int getCurr() {  
  93.         return curr;  
  94.     }  
  95.   
  96.     public void setCurr(int curr) {  
  97.         this.curr = curr;  
  98.     }  
  99.   
  100.     public int getTotal() {  
  101.         return total;  
  102.     }  
  103.   
  104.     public void setTotal(int total) {  
  105.         this.total = total;  
  106.     }  
  107.   
  108.     public String getHref() {  
  109.         return href;  
  110.     }  
  111.   
  112.     public void setHref(String href) {  
  113.         this.href = href;  
  114.     }  
  115.   
  116.     public String getCparam() {  
  117.         return cparam;  
  118.     }  
  119.   
  120.     public void setCparam(String cparam) {  
  121.         this.cparam = cparam;  
  122.     }  
  123.   
  124.     public String getSparam() {  
  125.         return sparam;  
  126.     }  
  127.   
  128.     public void setSparam(String sparam) {  
  129.         this.sparam = sparam;  
  130.     }  
  131.   
  132.     public int getSize() {  
  133.         return size;  
  134.     }  
  135.   
  136.     public void setSize(int size) {  
  137.         this.size = size;  
  138.     }  
  139.   
  140. }  

在jsp中使用自定义的标签库来实现翻页功能:

list.jsp

[javascript]  view plain copy
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8" %>  
  3. <%@ page session="false"%>  
  4. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>  
  5. <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>  
  6. <%@ taglib uri="my-taglib" prefix="my"%>  
  7. <title>学生列表</title>  
  8. <form action="list" method="post">  
  9. <table>  
  10. <tr>  
  11.     <td width="160">id</td>  
  12.     <td width="160">姓名</td>  
  13.     <td width="160">性别</td>  
  14.     <td width="160">年龄</td>  
  15.     <td width="160">生日</td>  
  16. </tr>  
  17. <c:forEach items="${rf.items}" var="student">  
  18. <tr>  
  19.     <td><c:out value="${student.id}"/></td>  
  20.     <td><c:out value="${student.name}"/></td>  
  21.     <td><c:out value="${student.gender}"/></td>  
  22.     <td><c:out value="${student.age}"/></td>  
  23.     <td><fmt:formatDate value="${student.birthday}"  type="both" pattern="yyyy-MM-dd"/></td>  
  24. </tr>  
  25. </c:forEach>  
  26. </table>  
  27. <my:paging curr="${rf.currentPage}" total="${rf.pageCount}" size="${rf.pageSize}" href="list"/>  
  28. </form>  

controller中根据参数对ResultFilter进行设置:

[java]  view plain copy
  1. @RequestMapping(value="/list")  
  2.     public ModelAndView listStudent(@ModelAttribute ResultFilter<Student> rf){  
  3.         studentService.listStudent(rf);  
  4.         return new ModelAndView("list""rf", rf);  
  5.     }  

service中的方法:

[java]  view plain copy
  1. public void listStudent(ResultFilter<Student> rf){  
  2.     rf.setTotalCount(getStudentCount());  
  3.     rf.setItems(studentDao.getListForPage("from Student", (rf.getCurrentPage() - 1 )* rf.getPageSize(), rf.getPageSize()));  
  4. }  

这个方法没有使用session,能支持多台服务器共同提供服务的情况。但是这个方法的效率可能有点低,因为每次翻页的时候都会重新创建一个ResultFilter,使用hibernate的缓存应该会使性能有所提高。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值