参考资料
1 ssh分页(多个例子)
http://useryouyou.iteye.com/blog/593954
2 ssh2分页例子
http://459104018-qq-com.iteye.com/blog/467196
3 ssh2分页
http://blog.csdn.net/shmily2038/archive/2009/12/28/5090044.aspx
注意事项:
此示例是在:Struts2.2+Spring3.1+Hibernate3.6整合(登陆示例及CRUD操做)基础上加的分页功能:
http://liuzidong.iteye.com/blog/935493
实现功能:分页,排序,设置每页显示多少条,转到第多少页
调用说明:
1 导入只须要:com.liuzd.page包下的类或者将page.jar加入WEB-INF/lib下也行^_^
2 Struts2前台类实现: BaseAction父类
3 在子类方法中调用父类提供的方法:
Page page = executePage(querySql,totalCount," id desc ");
须要传递三个参数就好了: querySql(查询sql语句), totalCount(总行数),
" id desc "(排序的列名与排序方式)
4 返回分页数据
List<User> users = this.userService.getUserListByPage(page);
5 在spring配置文件中请保持有: jdbcTemplate与hibernateTemplate这二个Bean的名字,不然dbUtil类不能使用
6 具体可参见:四的说明
一 运行环境:XP+Myeclipse6.6+WebLogic92+Oracle10g
二 工程相关图片:
1 DEMO图片

2 工程代码图片
3 page.jar图片
三 此示例是在:
Struts2.2+Spring3.1+Hibernate3.6整合(登陆示例及CRUD操做)基础上加的分页功能:
http://liuzidong.iteye.com/blog/935493,jar包在此页面中下载
四 关注类及页面:
1 BaseAction类(可能你在项目有其它的父类要使用,只要关注这个类中的: protected Page executePage(String querySql,Long totalCount,String columnNameDescOrAsc)方法就好了,方法中的参数不用修改,它来自于page.jsp,你可拷贝这个方法到你的父类中就实现了分页功能,分页类详见注释)
2 UserAction子类(只关注:方法:userList()中的调用方式)
3 UserDAOImpl类(关注方法:public List<User> getUserListByPage(final Page page) )
4 userList.jsp页面 html
- <%@ taglib uri="/WEB-INF/c.tld" prefix="c"%>
- <c:set var="page" value="${sessionScope.page}" />
在排序处: java
- <font color='red'>${page.sortName eq "username" ? page.sortInfo :page.defaultInfo}</font>
在下面加上: web
- <jsp:include page="/page/page.jsp">
- <jsp:param name="url" value="userAction!userList.action" />
- <!-- 演示传值:要用%26 -->
- <jsp:param name="urlParams" value="%26age=2" />
- </jsp:include>
四 要关注的类与页面
1 BaseAction.java
spring
- package com.liuzd.common;
-
- import java.util.Map;
-
- import javax.servlet.http.HttpServletRequest;
-
- import org.apache.struts2.ServletActionContext;
- import org.apache.struts2.interceptor.SessionAware;
-
- import com.liuzd.page.Page;
- import com.liuzd.page.PageUtil;
- import com.liuzd.page.PageState;
- import com.opensymphony.xwork2.ActionContext;
- import com.opensymphony.xwork2.ActionSupport;
-
- public class BaseAction extends ActionSupport implements SessionAware{
-
- private static final long serialVersionUID = 1L;
-
- public void setSession(Map<String, Object> sessionMap) {
-
- }
-
- protected Map<String,Object> getMapSession(){
- return (Map<String,Object>)ActionContext.getContext().getSession();
- }
-
- protected Object getMapSessionGet(String key){
- return getMapSession().get(key);
- }
-
- protected void setMapSessionPut(String key,Object value){
- getMapSession().put(key, value);
- }
-
-
- protected HttpServletRequest getRequest(){
- return ServletActionContext.getRequest ();
- }
-
-
- protected javax.servlet.http.HttpSession getSession(){
- return getRequest().getSession();
- }
-
-
- protected void setRequestAttribute(String attribute,Object attrValue){
- getRequest().setAttribute(attribute, attrValue);
- }
-
-
- protected Object getRequestAttribute(String attribute){
- return getRequest().getAttribute(attribute);
- }
-
-
- protected void setSessionAttribute(String attribute,Object attrValue){
- getSession().setAttribute(attribute, attrValue);
- }
-
-
- protected Object getSessionAttribute(String attribute){
- return getSession().getAttribute(attribute);
- }
-
-
-
- protected Page executePage(String querySql,Long totalCount,String columnNameDescOrAsc){
- String oracleSql = PageUtil.createQuerySql(querySql,columnNameDescOrAsc);
- if(null == totalCount){
- totalCount = 0L;
- }
-
- String pageAction = getRequest().getParameter("pageAction");
- String value = null;
-
- int index = PageState.getOrdinal(pageAction);
- if(0 == index){
-
- value = getRequest().getParameter("everyPage");
- }
-
- Page page = null;
-
- if(index < 1){
- page = PageUtil.inintPage(oracleSql,totalCount,index,value,getPage());
- }else{
-
- if(5 == index){
- value = getRequest().getParameter("sortName");
-
- }else if(6 == index){
- value = getRequest().getParameter("currentPage");
- }
- page = PageUtil.execPage(index,value,getPage());
- }
- setSession(page);
- return page;
- }
-
- private Page getPage() {
- Page page = (Page)getSession().getAttribute(PageUtil.SESSION_PAGE_KEY);
- if(page == null){
- page = new Page();
- }
- return page;
- }
-
- private void setSession(Page page) {
- getSession().setAttribute(PageUtil.SESSION_PAGE_KEY,page);
- }
- }
2 UserAction.java
sql
3 UserDAOImpl.java
apache
5 JSP引用
session
- <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
- <%@ taglib prefix="s" uri="/struts-tags"%>
- <%@ taglib uri="/WEB-INF/c.tld" prefix="c"%>
- <c:set var="page" value="${sessionScope.page}" />
- <html>
- <head>
- <title>用户集合</title>
- </head>
-
- <body>
- <table width="60%" border="1" cellpadding="0" align="center">
- <thead>
- <tr>
- <th style="cursor: hand;" title="按姓名进行排序" onclick="sortPage('username')" valign="top">
- 姓名<font color='red'>${page.sortName eq "username" ? page.sortInfo : page.defaultInfo}</font>
- </th>
- <th style="cursor: hand;" title="按年龄进行排序" onclick="sortPage('age')" valign="top">
- 年龄<font color='red'>${page.sortName eq "age" ? page.sortInfo : page.defaultInfo}</font>
- </th>
- <th style="cursor: hand;" title="按性别进行排序" onclick="sortPage('sex')" valign="top">
- 性别<font color='red'>${page.sortName eq "sex" ? page.sortInfo : page.defaultInfo}</font>
- </th>
- <th style="cursor: hand;" title="按地址进行排序" onclick="sortPage('address')" valign="top">
- 地址<font color='red'>${page.sortName eq "address" ? page.sortInfo : page.defaultInfo}</font>
- </th>
- <th style="cursor: hand;" >
- 操做
- </th>
- </tr>
- </thead>
- <tbody>
- <!--
- <s:iterator value="#request.userList" status="status" >
- <tr align="center">
- <td><s:property value="username"/></td>
- <td><s:property value="age"/></td>
- <td><s:property value="sex"/></td>
- <td><s:property value="address"/></td>
- <td>
- <s:a href="userAction!addUser.action">添加</s:a> | <s:a href="userAction!loadUser.action?user.id=%{id}">编辑</s:a> |
- <a href="<s:url action="userAction!delUser.action"><s:param name="user.id" value="id"/></s:url>">删除</a>
- </td>
- </tr>
- </s:iterator>
- -->
-
- <c:forEach items="${requestScope.userList}" var="user">
- <tr align="center">
- <td>
- ${user.username}
- </td>
- <td>
- ${user.age}
- </td>
- <td>
- ${user.sex eq 1 ? "男" : user.sex eq 2 ? "女" : "未知"}
- </td>
- <td>
- ${user.address}
- </td>
- <td>
- <a
- href="${pageContext.request.contextPath}/userAction!addUser.action">添加</a>
- |
- <a
- href="${pageContext.request.contextPath}/userAction!loadUser.action?user.id=${user.id}">编辑</a>
- |
- <a
- href="${pageContext.request.contextPath}/userAction!delUser.action?user.id=${user.id}">删除</a>
- </td>
- </tr>
- </c:forEach>
-
- <jsp:include page="page.jsp">
- <jsp:param name="url" value="userAction!userList.action" />
- <!-- 演示传值:要用%26 -->
- <jsp:param name="urlParams" value="%26age=2" />
- </jsp:include>
-
- </tbody>
- </table>
- <br>
- <a href="${pageContext.request.contextPath}/Login.jsp">返回</a>
- <br>
- <s:debug></s:debug>
- </body>
- </html>
五 你不须要关注的分页类与JSP页面,可在附件下载jar与源码
1 Page.java
oracle
2 PageState.java
app
- package com.liuzd.page;
-
- import org.apache.commons.lang3.StringUtils;
-
- public enum PageState {
-
-
- SETPAGE,
-
- FIRST,
-
- PREVIOUS,
-
- NEXT,
-
- LAST,
-
- SORT,
-
- GOPAGE;
-
-
-
- public static int getOrdinal(String value) {
- int index = -1;
- if (StringUtils.isEmpty(value)) {
- return index;
- }
- String newValue = StringUtils.trim(value).toUpperCase();
- try {
- index = valueOf(newValue).ordinal();
- } catch (IllegalArgumentException e) {}
- return index;
- }
- }
3 PageUtil.java
eclipse
- package com.liuzd.page;
-
- public class PageUtil {
-
- public static final String ASC = "asc";
- public static final String DESC = "desc";
- public static final String PAGE_DESC = "↓";
- public static final String PAGE_ASC = "↑";
- public static final String PAGE_NULL = " ";
- public static final String SESSION_PAGE_KEY = "page";
-
-
-
- public static String getPageQuerySql(String querySql,Long beginIndex, Long endinIndex) {
- if(querySql.indexOf("where rn>") != -1 && querySql.indexOf("and rn<=") != -1){
- return querySql.toUpperCase();
- }
- return new java.lang.StringBuffer().append(querySql).append(" where rn>").append(beginIndex).append(" and rn<=").append(endinIndex).toString().toUpperCase();
- }
-
-
- @SuppressWarnings("unused")
- public static String createQuerySql(String querySql, String orderByName) {
- StringBuilder sql = new StringBuilder();
- sql.append("select ttt.* from(select tt.*,rownum rn from(");
- sql.append(querySql);
- if (org.apache.commons.lang3.StringUtils.isNotEmpty(orderByName)) {
- sql.append(" order by ").append(orderByName);
- }
- sql.append(" )tt)ttt ");
- return sql.toString();
- }
-
-
- public static String getSortDescOrAsc(String querySql) {
-
- querySql = querySql.toUpperCase();
- String temp = "ORDER BY";
- int orderIndex = querySql.lastIndexOf(temp);
- String newsql = querySql.substring(orderIndex);
- String temp2 = ")";
- int lastIndex = newsql.indexOf(temp2);
- String orderByName = newsql.substring(temp.length(),lastIndex).trim();
- return orderByName;
- }
-
-
- public static Page inintPage(String initPageSql,Long totalCount,Integer index,String value,Page sessionPage){
- Page page = null;
- if(index < 0){
- page = new Page(totalCount);
- }else{
-
- Long everPage = null == value ? 10 : Long.parseLong(value);
-
- page = sessionPage;
- page.setEveryPage(everPage);
- page.setTotalCount(totalCount);
- }
- page.setInitQuerySql(initPageSql);
-
- String querySql = getPageQuerySql(initPageSql,page.getBeginIndex(), page.getEndinIndex());
-
- page.setQuerySql(querySql);
-
- return page;
- }
-
-
-
-
-
- public static Page execPage(int index,String value,Page sessionPage){
-
- Page page = sessionPage;
-
-
- page.pageState(index,value);
-
- String initPageSql = page.getInitQuerySql();
-
- String querySql = getPageQuerySql(initPageSql,page.getBeginIndex(), page.getEndinIndex());
-
-
- if (page.isSort() == true) {
- String sortName = page.getSortName();
- if (null != sortName) {
-
- String descAsc = page.getSortState();
-
- String sortNameDescAsc = (" " + sortName + " " + descAsc).toUpperCase();
-
- String getOldSortName = PageUtil.getSortDescOrAsc(querySql);
-
- querySql = querySql.replace(getOldSortName,sortNameDescAsc);
- }
- }
- page.setQuerySql(querySql);
- return page;
- }
-
- }
4 page.jsp
- <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
- <%@ taglib uri="/WEB-INF/c.tld" prefix="c"%>
- <c:set var="page" value="${sessionScope.page}" />
- <c:set var="path" value="${pageContext.request.contextPath}" />
- <c:set var="url" value="${param.url}" />
- <c:set var="urlParams" value="${param.urlParams}" />
- <c:set var="pathurl" value="${path}/${url}" />
- <tr>
- <td colspan="5">
- 共${page.totalCount}条记录 共${page.totalPage}页 每页显示${page.everyPage}条
- 当前第${page.currentPage}页
- <c:choose>
- <c:when test="${page.hasPrePage eq false}">
- <<首页 <上页
- </c:when>
- <c:otherwise>
- <a href="${pathurl}?&pageAction=first${urlParams}"><<首页 </a>
- <a href="${pathurl}?pageAction=previous${urlParams}" /><上一页</a>
- </c:otherwise>
- </c:choose>
- ||
- <c:choose>
- <c:when test="${page.hasNextPage eq false}">
- 下页> 尾页>>
- </c:when>
- <c:otherwise>
- <a href="${pathurl}?&pageAction=next${urlParams}">下一页> </a>
- <a href="${pathurl}?pageAction=last${urlParams}" />末页>></a>
- </c:otherwise>
- </c:choose>
-
- <SELECT name="indexChange" id="indexChange"
- onchange="getCurrentPage(this.value);">
- <c:forEach var="index" begin="1" end="${page.totalPage}" step="1">
- <option value="${index}" ${page.currentPage eq index ? "selected" : ""}>
- 第${index}页
- </option>
- </c:forEach>
- </SELECT>
-
- 每页显示:<select name="everyPage" id="everyPage" onchange="setEveryPage(this.value);">
- <c:forEach var="pageCount" begin="5" end="${page.totalCount}" step="5">
- <option value="${pageCount}" ${page.everyPage eq pageCount ? "selected" : ""}>
- ${pageCount}条
- </option>
- </c:forEach>
- </select>
- </td>
- </tr>
- <div style='display: none'>
- <a class=listlink id="indexPageHref" href='#'></a>
- </div>
- <script>
- function getCurrentPage(index){
- var a = document.getElementById("indexPageHref");
- a.href = '${pathurl}?pageAction=gopage¤tPage='+index+'${urlParams}';
- a.setAttribute("onclick",'');
- a.click("return false");
- }
- function setEveryPage(everyPage){
- var a = document.getElementById("indexPageHref");
- var currentPage = document.getElementById('indexChange').value;
- a.href = '${pathurl}?pageAction=setpage&everyPage='+everyPage+'${urlParams}';
- a.setAttribute("onclick",'');
- a.click("return false");
- }
- function sortPage(sortName){
- var a = document.getElementById("indexPageHref");
- a.href = '${pathurl}?pageAction=sort&sortName='+sortName+'${urlParams}';
- a.setAttribute("onclick",'');
- a.click("return false");
- }
- </script>
- 原地址是 http://liuzidong.iteye.com/blog/1042172