spring mvc 分页

分页主要需要两个参数:

1、当前页是第几页

2、每页展示多少条数据

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

package com.smvc.annonation.utils;

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 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 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 getItems() {
        return items;
    }

    public void setItems(List 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);
	}

}

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

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

自定义标签的配置:

my.tld




	My Tag Library
	1.0
	my
	my-taglib

	
		split page
		paging
		com.smvc.annonation.tag.PagingTag
		empty
		
			base href
			href
			false
			true
		
		
			curr page
			curr
			true
			true
		
		
			page size
			size
			true
			true
		
		
			total page
			total
			true
			true
		
		
			curr parameter name
			cparam
			false
			false
		
		
			page size parameter name
			sparam
			false
			false
		
		false
	
	
	
	
	
		front split page
		frontPaging
		com.aspire.cms.demo.framework.tag.FrontPagingTag
		empty
		
			base href
			href
			false
			true
		
		
			result filter
			rf
			true
			true
		
		false
	

对应的java类:

package com.smvc.annonation.tag;

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("");
		// 首页
		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("");
		out.append("第");
		out.append(curr + "/" + total);
		out.append("页");
		
		
		super.doTag();
	}
	
	private void href(JspWriter out, String href, int curr, String title) throws IOException {
		out.append("").append(title).append("");
	}

	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中使用自定义的标签库来实现翻页功能:

list.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" %>
<%@ page session="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib uri="my-taglib" prefix="my"%>
学生列表
id 姓名 性别 年龄 生日

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

@RequestMapping(value="/list")
	public ModelAndView listStudent(@ModelAttribute ResultFilter rf){
		studentService.listStudent(rf);
		return new ModelAndView("list", "rf", rf);
	}

service中的方法:

	public void listStudent(ResultFilter rf){
		rf.setTotalCount(getStudentCount());
		rf.setItems(studentDao.getListForPage("from Student", (rf.getCurrentPage() - 1 )* rf.getPageSize(), rf.getPageSize()));
	}

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

你可能感兴趣的:(JAVA)