Spring3.x_Struts2.x_Hibernate3.x整合之分页pager-taglib的简单使用
此文章是在Spring3.x_Struts2.x_Hibernate3.x整合之用户管理例子笔记 的基础上完成!
导入pager-taglib的jar包:pager-taglib-2.0.jar
首先创建分页对象:
<span style="font-family:FangSong_GB2312;font-size:14px;">package org.oms.spring.model.pager; import java.util.List; public class Pager<T> { /** * 分页数据集 */ private List<T> datas; /** * 当前页 */ private int pageOffset; /** * 总条数 */ private long totalRecord; /** * 每页显示多少条 */ private int pageSize; public List<T> getDatas() { return datas; } public void setDatas(List<T> datas) { this.datas = datas; } public int getPageOffset() { return pageOffset; } public void setPageOffset(int pageOffset) { this.pageOffset = pageOffset; } public long getTotalRecord() { return totalRecord; } public void setTotalRecord(long totalRecord) { this.totalRecord = totalRecord; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } } </span>
使用ThreadLocal方式获取参数:
<span style="font-family:FangSong_GB2312;font-size:14px;">package org.oms.spring.model.pager; public class SystemContext { private static ThreadLocal<Integer> pageOffset = new ThreadLocal<Integer>(); private static ThreadLocal<Integer> pageSize = new ThreadLocal<Integer>(); public static int getPageOffset() { return pageOffset.get(); } public static void setPageOffset(int _pageOffset) { pageOffset.set(_pageOffset); } public static int getPageSize() { return pageSize.get(); } public static void setPageSize(int _pageSize) { pageSize.set(_pageSize); } public static void removePageOffset() { pageOffset.remove(); } public static void removePageSize() { pageSize.remove(); } } </span>
<span style="font-family:FangSong_GB2312;font-size:14px;">package org.oms.spring.filter; 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 org.oms.spring.model.pager.SystemContext; public class SystemContextFilter implements Filter { private int pageSize = 10; @Override public void destroy() { } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { try { int pageOffset = 0; try { String pagerParam = request.getParameter("pager.offset"); pageOffset = Integer.parseInt(pagerParam); } catch (NumberFormatException e) { } SystemContext.setPageOffset(pageOffset); SystemContext.setPageSize(pageSize); chain.doFilter(request, response); } finally { SystemContext.removePageOffset(); SystemContext.removePageSize(); } } @Override public void init(FilterConfig config) throws ServletException { try { pageSize = Integer.parseInt(config.getInitParameter("pageSize")); } catch (NumberFormatException e) { pageSize = 10; } } } </span>
<span style="font-family:FangSong_GB2312;font-size:14px;"><filter> <filter-name>SystemContextFilter</filter-name> <filter-class>org.oms.spring.filter.SystemContextFilter</filter-class> <init-param> <description>分页参数</description> <param-name>pageSize</param-name> <param-value>10</param-value> </init-param> </filter> <filter-mapping> <filter-name>SystemContextFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </span>
<span style="font-family:FangSong_GB2312;font-size:14px;">public Pager<T> find(String hql,Object[] args); public Pager<T> find(String hql,Object args); public Pager<T> find(String hql); </span>
<span style="font-family:FangSong_GB2312;font-size:14px;">private String getCountHql(String hql){ //1.获取from 前面的字符串 String f=hql.substring(0,hql.indexOf("from")); //2.将from前面的字符串替换为select count(*) if (f.equals("")) { hql="select count(*) "+hql; }else { hql=hql.replace(f, "select count(*) "); } //3.将fetch 替换为"",因为抓取查询不能使用count(*) hql=hql.replace("fetch", ""); return hql; } @Override public Pager<T> find(String hql, Object args) { return this.find(hql, new Object[]{args}); } @Override public Pager<T> find(String hql) { return this.find(hql, null); }</span>
<span style="font-family:FangSong_GB2312;font-size:14px;">public String list(){ ActionContext.getContext().put("us", userService.findUser()); return SUCCESS; }</span>
创建公共的pager分页页面,可以将次页面引入需要分页的页面中
<span style="font-family:FangSong_GB2312;font-size:14px;"><%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="pg" uri="http://jsptags.com/tags/navigation/pager" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <pg:pager items="${param.items }" url="${param.url }" maxPageItems="10" export="currentPageNumber=pageNumber"> <pg:first> <a href="${pageUrl }">首页</a> </pg:first> <pg:prev> <a href="${pageUrl }">前页</a> </pg:prev> <pg:pages> <c:choose> <c:when test="${currentPageNumber eq pageNumber}"> <font color="red">${pageNumber }</font> </c:when> <c:otherwise> <a href="${pageUrl }">${pageNumber }</a> </c:otherwise> </c:choose> </pg:pages> <pg:next> <a href="${pageUrl }">下页</a> </pg:next> <pg:last> <a href="${pageUrl }">尾页</a> </pg:last> </pg:pager> </span>
使用是引入脚本方法
<span style="font-family:FangSong_GB2312;font-size:14px;"><!-- 插入分页导航条 --> <jsp:include page="/including/pager.jsp"> <jsp:param value="user_list.action" name="url"/> <jsp:param value="${us.totalRecord }" name="items"/> </jsp:include></span>
<span style="font-family:FangSong_GB2312;font-size:14px;"><!-- 可以在这里插入分页导航条 --> <s:include value="/common/pager.jsp"> <s:set var="url" value="'org.action'"/> <s:set var="params" value="#{'parentId':null}"/> </s:include> </span>
<span style="font-family:FangSong_GB2312;font-size:14px;">在处理之前先对pager-taglib的标签结构进行了解. <pg:pager> <pg:item></pg:item> <pg:index> <pg:first></pg:first> <pg:prev></pg:prev> <pg:pages></pg:pages> <pg:next></pg:next> <pg:last></pg:last> </pg:index> </pg:pager> pg:pager:这个标签用来设置分页的总体参数. pg:first:第一页的标签. pg:prev:上一页标签. pg:next:下一页标签. pg:last:最后一页标签. pg:pages:这个标签用来循环输出页码信息.</span>
<span style="font-family:FangSong_GB2312;font-size:14px;">jstl.jar 和 standard.jar</span>
测试结果图:
个人备忘笔记~~2014年10月20日00:27:35