1、以下为分页类:

import java.io.Serializable;  
import java.util.List;  
 
import org.apache.commons.lang.builder.ToStringBuilder;  
import org.apache.commons.lang.builder.ToStringStyle;  
 
public class ResultFilter<T> implements Serializable{  
 
    private static final long serialVersionUID = 5472321653620726832L;  
 
    private final static int DEFAULT_NAVIGATOR_SIZE = 5;  
 
    //当前页  
    private int currentPage = 1;  
    //每页显示数量  
    private int pageSize = 5;  
      
    //总条数  
    private int totalCount;  
 
    private boolean havaNextPage;  
 
    private boolean havePrePage;  
 
    private int navigatorSize;  
      
    //存放查询结果用的list  
    private List<T> items;  
      
    public ResultFilter(){  
          
    }  
      
    public ResultFilter(int totalCount, int pageSize, int currentPage) {  
        this(totalCount, pageSize, currentPage, DEFAULT_NAVIGATOR_SIZE);  
    }  
 
    public ResultFilter(int totalCount, int pageSize, int currentPage,  
                        int navigatorSize) {  
        this.totalCount = totalCount;  
        this.pageSize = pageSize;  
        this.currentPage = currentPage;  
        this.navigatorSize = navigatorSize;  
    }  
      
    public int getPageCount() {  
        int pageCount = 0;  
        if (pageSize != 0) {  
            pageCount = totalCount / pageSize;  
            if (totalCount % pageSize != 0)  
                pageCount++;  
        }  
 
        return pageCount;  
    }  
 
    public int getCurrentPage() {  
        currentPage = currentPage < getPageCount() ? currentPage :  
                      getPageCount();  
        currentPage = currentPage < 1 ? 1 : currentPage;  
 
        return currentPage;  
    }  
 
    public int getPageSize() {  
        return pageSize;  
    }  
 
    public int getTotalCount() {  
        return totalCount;  
    }  
 
 
    public boolean isHaveNextPage() {  
        havaNextPage = false;  
        if ((getPageCount() > 1) && (getPageCount() > getCurrentPage()))  
            havaNextPage = true;  
        return havaNextPage;  
    }  
 
    public boolean isHavePrePage() {  
        havePrePage = false;  
        if ((getPageCount() > 1) && (currentPage > 1))  
            havePrePage = true;  
        return havePrePage;  
    }  
 
    private int getNavigatorIndex(boolean isBegin) {  
        int beginNavigatorIndex = getCurrentPage() - navigatorSize / 2;  
        int endNavigatorIndex = getCurrentPage() + navigatorSize / 2;  
        beginNavigatorIndex = beginNavigatorIndex < 1 ? 1 : beginNavigatorIndex;  
        endNavigatorIndex = endNavigatorIndex < getPageCount() ?  
                            endNavigatorIndex :  
                            getPageCount();  
        while ((endNavigatorIndex - beginNavigatorIndex) < navigatorSize &&  
               (beginNavigatorIndex != 1 || endNavigatorIndex != getPageCount())) {  
            if (beginNavigatorIndex > 1)  
                beginNavigatorIndex--;  
            else if (endNavigatorIndex < getPageCount())  
                endNavigatorIndex++;  
        }  
 
        if(isBegin)  
            return beginNavigatorIndex;  
        else  
            return endNavigatorIndex;  
    }  
 
    public int getBeginNavigatorIndex() {  
        return getNavigatorIndex(true);  
    }  
 
    public int getEndNavigatorIndex() {  
        return getNavigatorIndex(false);  
    }  
 
    public List<T> getItems() {  
        return items;  
    }  
 
    public void setItems(List<T> items) {  
        this.items = items;  
    }  
      
    public void setCurrentPage(int currentPage) {  
        this.currentPage = currentPage;  
    }  
 
    public void setPageSize(int pageSize) {  
        this.pageSize = pageSize;  
    }  
 
    public void setTotalCount(int totalCount) {  
        this.totalCount = totalCount;  
    }  
 
    @Override  
    public String toString() {  
        return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);  
    }  
 
}

2、在spring的controller中调用分页类:

@RequestMapping(value="/xxxList")    //xxxList.jsp对应的controller
    public ModelAndView xxxList(@ModelAttribute ResultFilter<xxx> rf){
        systemService.listServiceAll(rf);                                               //调用步骤3 ,分页获取xxx对象
        return new ModelAndView("路径/xxxList", "rf", rf);      //此处跳转到步骤5所表示的网页,并传递分页参数rf,其中“路径/xxxList”

//表示对应的jsp网页,比如myBook/bookList表示myBook目录下的bookList.jsp
    }

3、连操作数据库的service层   获取全部service
    @Transactional(readOnly = false)
    public void listServiceAll(ResultFilter<Services> rf){
        int count=getServiceList().size();
        rf.setTotalCount(count);  
        rf.setItems(serviceDao.getListForPage("from Services", (rf.getCurrentPage() - 1 )* rf.getPageSize(), rf.getPageSize()));

//调用步骤4,分页查询数据库
    }

4、

// -------------- 分页QL Query --------------//
    
    @SuppressWarnings("unchecked")
    public List<T> getListForPage(String hql,int begin,int num){
         Query query = getSession().createQuery(hql);
        
        return query.setFirstResult(begin).setMaxResults(num).list();
        
    }

5、路径/xxxList 对应的jsp网页(注意第四行)

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<%@ taglib uri="my-taglib" prefix="my"%>               <!--使用了自定义标签,该标签在步骤6中定义-->

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>XXXList</title>
</head>
<body>

<form>    
        <table id="contentTable" >        
        <tbody>
            <c:url value="/xxxDetail" var="url1"></c:url>
            <c:url value="/xxxList" var="url1"></c:url>    <!--该jsp对应的controller-->
            <tr><c:forEach items="${rf.items}" var="xxx">
                <td valign="top" style="width:229px;oveflow:hidden;">

<table>
                       <tr><td ><a name="click to check the detail" href="${url1}?xxxId=${xxx.id}"><img src="${ctxStatic}/images/123.jpg"></img></a></td></tr>
                     <tr><td >${xxx.attribute1}</td></tr>
                     <tr><td >${xxx.attribute2}</td></tr>
                     </table>
                </td>
                </c:forEach>                                
            </tr>        
        </tbody>
    </table>
    <div style="padding-top:30px;"></div>
    <my:paging curr="${rf.currentPage}" total="${rf.pageCount}" size="${rf.pageSize}" href="${url2}"/>      <!--使用了自定义标签-->
    </form>
</body>
</html>

6、自定义标签‘my’

<?xml version="1.0" encoding="UTF-8"?>  
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"  
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"  
        version="2.0">  
 
    <description>My Tag Library</description>  
    <tlib-version>1.0</tlib-version>  
    <short-name>my</short-name>  
    <uri>my-taglib</uri>  
 
    <tag>  
        <description>split page</description>  
        <name>paging</name>  
        <tag-class>aaa.bbb.ccc.PagingTag</tag-class>     <!--用到了步骤7中的PagingTag类 ,aaa.bbb.ccc是PagingTag的路径-->
        <body-content>empty</body-content>  
        <attribute>  
            <description>base href</description>  
            <name>href</name>  
            <required>false</required>  
            <rtexprvalue>true</rtexprvalue>  
        </attribute>  
        <attribute>  
            <description>curr page</description>  
            <name>curr</name>  
            <required>true</required>  
            <rtexprvalue>true</rtexprvalue>  
        </attribute>  
        <attribute>  
            <description>page size</description>  
            <name>size</name>  
            <required>true</required>  
            <rtexprvalue>true</rtexprvalue>  
        </attribute>  
        <attribute>  
            <description>total page</description>  
            <name>total</name>  
            <required>true</required>  
            <rtexprvalue>true</rtexprvalue>  
        </attribute>  
        <attribute>  
            <description>curr parameter name</description>  
            <name>cparam</name>  
            <required>false</required>  
            <rtexprvalue>false</rtexprvalue>  
        </attribute>  
        <attribute>  
            <description>page size parameter name</description>  
            <name>sparam</name>  
            <required>false</required>  
            <rtexprvalue>false</rtexprvalue>  
        </attribute>  
        <dynamic-attributes>false</dynamic-attributes>  
    </tag>  
      
      
      
    <tag>  
        <description>front split page</description>  
        <name>frontPaging</name>  
        <tag-class>com.aspire.cms.demo.framework.tag.FrontPagingTag</tag-class>  
        <body-content>empty</body-content>  
        <attribute>  
            <description>base href</description>  
            <name>href</name>  
            <required>false</required>  
            <rtexprvalue>true</rtexprvalue>  
        </attribute>  
        <attribute>  
            <description>result filter</description>  
            <name>rf</name>  
            <required>true</required>  
            <rtexprvalue>true</rtexprvalue>  
        </attribute>  
        <dynamic-attributes>false</dynamic-attributes>  
    </tag>  
 
</taglib>

7、自定义标签类

package aaa.bbb.ccc;

import java.io.IOException;

import javax.servlet.jsp.JspException;  
import javax.servlet.jsp.JspWriter;  
import javax.servlet.jsp.tagext.SimpleTagSupport;  
 
import org.apache.commons.lang.StringUtils;  
 
public class PagingTag extends SimpleTagSupport {  
      
    private String href;  
      
    //当前页  
    private String cparam;  
    //每页条数  
    private String sparam;  
 
    private int curr;//当前页  
      
    private int size;//每页条数  
      
    private int total;//总页数  
      
    @Override  
    public void doTag() throws JspException, IOException {  
        JspWriter out = getJspContext().getOut();  
          
        if(StringUtils.isEmpty(cparam)) {  
            cparam = "currentPage";  
        }  
        if(StringUtils.isEmpty(sparam)) {  
            sparam = "pageSize";  
        }  
          
        if(!href.endsWith("?") && !href.endsWith("&")) {  
            if(href.indexOf("?") == -1) {  
                href = href + "?";  
            } else {  
                href = href + "&";  
            }  
        }  
          
        if (curr <= 0) {  
            curr = 1;  
        } else if (curr > total) {  
            curr = total;  
        }  
          
        out.append("<span>");  
        // 首页  
        if (curr == 1) {  
            out.append("首页");  
        } else {  
            href(out, href, 1, "首页");  
        }  
        out.append(" | ");  
        // 上一页  
        if (curr == 1) {  
            out.append("上一页");  
        } else {  
            href(out, href, curr - 1, "上一页");  
        }  
        out.append(" | ");  
        // 下一页  
        if (curr == total) {  
            out.append("下一页");  
        } else {  
            href(out, href, curr + 1, "下一页");  
        }  
        out.append(" | ");  
        // 末页  
        if (curr == total) {  
            out.append("末页");  
        } else {  
            href(out, href, total, "末页");  
        }  
        out.append("</span>");  
        out.append("<span>第");  
        out.append(curr + "/" + total);  
        out.append("页</span>");  
          
          
        super.doTag();  
    }  
      
    private void href(JspWriter out, String href, int curr, String title) throws IOException {  
        out.append("<a href=\"").append(href).append(cparam).append("=").append("" + curr).append("&").append(sparam).append("=").append("" + size).append("\">").append(title).append("</a>");  
    }  
 
    public int getCurr() {  
        return curr;  
    }  
 
    public void setCurr(int curr) {  
        this.curr = curr;  
    }  
 
    public int getTotal() {  
        return total;  
    }  
 
    public void setTotal(int total) {  
        this.total = total;  
    }  
 
    public String getHref() {  
        return href;  
    }  
 
    public void setHref(String href) {  
        this.href = href;  
    }  
 
    public String getCparam() {  
        return cparam;  
    }  
 
    public void setCparam(String cparam) {  
        this.cparam = cparam;  
    }  
 
    public String getSparam() {  
        return sparam;  
    }  
 
    public void setSparam(String sparam) {  
        this.sparam = sparam;  
    }  
 
    public int getSize() {  
        return size;  
    }  
 
    public void setSize(int size) {  
        this.size = size;  
    }  
 
}

jsp分页技术的更多相关文章

  1. JSP分页技术的实现(利用当前页进行前后加减,并利用href进行当前页面传值,传值当然是那个当前值变量)

    一.可滚动结果集   Connection con  = DriverManager.getConnection(); PreparedStatement stmt = con.prepareStat ...

  2. 眼下最好的JSP分页技术

     2005-08-24   来源:CSDN  作者:wanchao2001 前言     在使用数据库的过程中,不可避免的须要使用到分页的功能,但是JDBC的规范对此却没有非常好的解决.对于这个需求非 ...

  3. jsp-------------之分页技术(一)

    jsp分页技术之: 如下图:百度的喵 看上图中卡哇伊的小苗的爪子下面的数字,就是分页啦!那我们如何做出这样一个效果呢? 下面我们来逐一分解: jsp分页技术一 :  (算法) /* int pageS ...

  4. JSP的分页技术

    在实际应用中,如果从数据库中查询的记录特别的多,甚至超过了显示屏的显示范围,这个时候可将结果进行分页显示. 假设总记录数为intRowCount,每页显示的数量为inPageSize,总页数为intP ...

  5. Javaweb 第15天 web练习和分页技术

    第15天 web练习和分页技术 复习day14内容: 学习新技术的思路? 分析功能的思路? 使用queryRunner操作数据库的步骤? ResultSetHandler接口常用实现类(三个重点)? ...

  6. Jsp分页的简单制作

    Jsp分页的简单制作 运行环境:jsp+tomcat+eclipse 技术:servlet+jsp+mysql 分页技术还区分两个:假分页和真分页 假分页:一次性从数据库读出表的所有数据一次性的返回给 ...

  7. 改进Spring中的分页技术

    Spring中有一个PagedListHolder,能够实现分页. 但此类有几个缺点: 1. 使用此类的代码比較繁琐 2. 此类存放的数据源是全部的记录集,即对于记录数为1000条的数据,即使我们仅仅 ...

  8. JAVAEE之-----MySQL分页技术(带搜索)

    需求: 为什么须要採用分页技术呢?在数据库中我们查询数据的时候,须要将数据返回到显示页面.数据库中含有大量数据,所有显示在一个页面过于太多,所以我们须要採用分页技术.每一页显示不同数据. 主要解决这个 ...

  9. Mysql学习总结(32)——MySQL分页技术详解

    1.什么是数据分页:数据分页就是将很多条记录像书本一样分页,每页显示多少行记录: 2.为什么要数据分页:当我们进行sql语句查询时,假如数据有成千上万行记录,如果在同一个页面去显示,那这个页面得有多大 ...

随机推荐

  1. runtime重写description方法打印model属性和值

    在开发过程中, 往往会有很多的model来装载属性. 而在开发期间经常会进行调试查看model里的属性值是否正确. 那么问题来了, 在objective-c里使用NSLog("%@" ...

  2. 数据库的CRUD操作

    一:数据库的CRUD操作,C是指create新增,R是指retrieve检索,U是指update更改,D是指delete删除 SQL语句分为3类: 1.DDL指数据定义语言如:create,drop, ...

  3. jquery中Live方法不可用,Jquery中Live方法失效

    jquery中Live方法不可用,Jquery中Live方法失效 >>>>>>>>>>>>>>>>> ...

  4. Windows10 安装配置IIS,并将程序发布到服务器上

    1.确保计算机链接网络(也可在不联网的时候使用安装包进行IIS的安装): 2.打开“控制面板”(“菜单”按钮+x 快捷键)——“程序”——“打开或关闭Windows功能”——展开“Internet信息 ...

  5. Synchronized vs SyncRoot

    我们知道,在.net的一些集合类型中,譬如Hashtable和ArrayList,都有Synchronized静态方法和SyncRoot实例方法,他们之间有联系吗?我怎么才能用好他们呢?我们以Hash ...

  6. Winform获取应用程序的当前路径

    //获取当前进程的完整路径,包含文件名(进程名). string str = this.GetType().Assembly.Location; result: X:\xxx\xxx\xxx.exe ...

  7. JAVA-线程安全性

    线程安全性: 一个类是线程安全的是指在被多个线程访问时,类可以持续进行正确的行为.不用考虑这些线程运行时环境下的调度和交替.   编写正确的并发程序的关键在于对共享的,可变的状态进行访问管理. 解决方 ...

  8. java基础(死循环退出选项)

    java程序中为了程序正常运行,需要给无限循环加入一个退出选项,保证程序的可执行性. import java.util.Scanner; public class { public static vo ...

  9. Problem 1010 - 素数环问题

    #include<iostream> #include<string> #include<algorithm> #include<vector> #in ...

  10. 接触.net5年了,感觉自己的知识面很狭隘。

    08年毕业找工作期间开始接触网页开发,由于在学校了混了4年时间,我只能从html标记语言开始学习,后来应聘到一个网站建设公司,开始学习ps.Dreamweaver和asp.由于基础薄弱,一个月后离开了 ...