基於SSH+pager-taglib的分頁封裝 – JAVA編程語言程序開發技術文章

 手動編寫分頁挺好,不過太累。於是在搜索瞭很多網上開源分頁插件後選擇pager-taglib-2.0,此分頁插件因為用法簡單功能強大能快速上手,下面逐步實現快速jsp頁面的分頁。
整體效果(因為記錄比較少,所以隻有兩頁):



 


第二頁:



1.下載pager-taglib-2.0.war
    在http://jsptags.com/tags/navigation/pager/上可以看到demo和war包下載,在war中解壓出jar包,粘貼入項目的lib文件夾中。



2.在顯示的jsp上需要引入
<%@ taglib prefix=”pg”  uri=”http://jsptags.com/tags/navigation/pager” %>


3.建立JSPPageModel模型,用於在jsp頁面上返回對象集和總記錄數
package com.v.oa.common;


import java.util.List;


public class JSPPageModel {
   
    private int total;
   
    private List list;


    public int getTotal() {
        return total;
    }


    public void setTotal(int total) {
        this.total = total;
    }


    public List getList() {
        return list;
    }


    public void setList(List list) {
        this.list = list;
    }
   
   


}
4.建立SystemContext類,將系統啟動時顯示起始記錄數和每頁顯示行數設置在ThreadLocal中。
package com.v.oa.common;


public class SystemContext {
   
        private static ThreadLocal offset = new ThreadLocal();
        private static ThreadLocal pagesize = new ThreadLocal();
       
        public static void setOffset(int _offset){
            offset.set(_offset);
        }
       
        public static int getOffset(){
            Integer _offset = (Integer)offset.get();
            if(_offset == null){
                return 0;
            }
            return _offset;
        }
       
        public static void removeOffset(){
            offset.remove();
        }
       
        public static void setPagesize(int _pagesize){
            pagesize.set(_pagesize);
        }
       
        public static int getPagesize(){
            Integer _pagesize = (Integer)pagesize.get();
            if(_pagesize == null){
                return Integer.MAX_VALUE;
            }
            return _pagesize;
        }
       
        public static void removePagesize(){
            pagesize.remove();
        }
}



5.建立過濾器,讀出ThreadLocal中的顯示的起始記錄數和每頁顯示行數。這裡沒有在界面中去設置用戶可以選擇每頁顯示多少條記錄的功能,此處固定為每頁顯示5條記錄。
package com.v.oa.common;


import java.io.IOException;


import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;


public class PagerFilter implements Filter {


    @Override
    public void destroy() {
        // TODO Auto-generated method stub


    }


    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest)request;
       
        SystemContext.setOffset(getOffset(httpRequest));
        SystemContext.setPagesize(getPagesize(httpRequest));
       
        try{
            chain.doFilter(request, response);
        }finally{
            SystemContext.removeOffset();
            SystemContext.removePagesize();
        }
    }
   
    private int getOffset(HttpServletRequest request){
        int offset = 0;
        try {
            offset = Integer.parseInt(request.getParameter(“pager.offset”));
        } catch (Exception ignore) {
        }
        return offset;
    }
   
    private int getPagesize(HttpServletRequest request){
        return 5;
    }


    public void init(FilterConfig arg0) throws ServletException {
    }
}


6.action中:
protected ActionForward unspecified(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        OrgForm of=(OrgForm)form;
        int parentId=of.getParentId();
       
&nb

發佈留言