- package com.yizhou.common.test;
- /**
- *
- * Copyright @ 2008 YIZHOU SOFTWARE Co. Ltd.
- * All right reserved.
- *
- * @author xuxinlong
- *
- * mail: longxx888@163.com
- *
- */
- public class PageBean {
- private int count = 0; // 记录总数
- private int pageSize = 20; // 每页显示记录数
- private int pageCount = 0; // 总页数
- private int page = 1; // 当前页数
- private String totalCountSQL;// 得到总记录数sql语句
- private String listSQL;// 得到查询记录sql语句
- public int getCount() {
- return count;
- }
- public void setCount(int count) {
- if (pageSize != 0) {
- pageCount = count / pageSize;
- if (count % pageSize != 0) {
- pageCount++;
- }
- }
- this.count = count;
- }
- public String getListSQL() {
- return listSQL;
- }
- public void setListSQL(String listSQL) {
- this.listSQL = listSQL;
- }
- public int getPage() {
- return page;
- }
- public void setPage(int page) {
- this.page = page;
- }
- public int getPageCount() {
- return pageCount;
- }
- public void setPageCount(int pageCount) {
- this.pageCount = pageCount;
- }
- public int getPageSize() {
- return pageSize;
- }
- public void setPageSize(int pageSize) {
- this.pageSize = pageSize;
- }
- public String getTotalCountSQL() {
- return totalCountSQL;
- }
- public void setTotalCountSQL(String totalCountSQL) {
- this.totalCountSQL = totalCountSQL;
- }
- }
新建个接口
- package com.yizhou.common.test;
- import java.io.Serializable;
- import java.util.List;
- /**
- *
- * Copyright @ 2008 NANJING YIZHOU SOFTWARE Co. Ltd.
- * All right reserved.
- *
- * @author xuxinlong
- *
- * mail: longxx888@163.com
- *
- */
- public interface PaginateInterface extends Serializable {
- public List getList(PageBean page);
- public String getToolsMenu(PageBean page);
- public Long getTotalCount(PageBean p, String str[], Object ob2[])
- throws Exception;
- public Long getTotalCount(PageBean page) throws Exception;
- public List getList(PageBean page, String str[], Object ob2[])
- throws Exception;
- }
- package com.yizhou.common.test;
- import java.util.ArrayList;
- import java.util.List;
- import org.hibernate.Query;
- import org.hibernate.Session;
- import org.springframework.orm.hibernate3.HibernateCallback;
- import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
- /**
- *分页核心类
- * Copyright @ 2008 NANJING YIZHOU SOFTWARE Co. Ltd.
- * All right reserved.
- *
- * @author xuxinlong
- *
- * mail: longxx888@163.com
- *
- */
- public class Paginate extends HibernateDaoSupport implements PaginateInterface {
- /**
- * 显示用的分页信息
- */
- public String getToolsMenu(PageBean p) {
- StringBuffer str = new StringBuffer("");
- int next, prev;
- prev = p.getPage() - 1;
- next = p.getPage() + 1;
- if (p.getPage() > 1) {
- str
- .append("<a href=\"#\" οnclick=\"document.forms(0).jumpPage.value=1;document.forms(0).submit();\">首页</a> ");
- } else {
- //str.append("<a href=\"#\">首页</a> ");
- str.append("首页 ");
- }
- if (p.getPage() > 1) {
- str.append("<a href=\"#\" οnclick='document.forms(0).jumpPage.value="
- + prev + ";document.forms(0).submit();'>上页</a> ");
- } else {
- //str.append("<a href=\"#\">上页</a> ");
- str.append("上页 ");
- }
- if (p.getPage() < p.getPageCount()) {
- str.append("<a href=\"#\" οnclick='document.forms(0).jumpPage.value="
- + next + ";document.forms(0).submit();'>下页</a> ");
- } else {
- //str.append("<a href=\"#\" >下页</a> ");
- str.append("下页 ");
- }
- if (p.getPageCount() > 1 && p.getPage() != p.getPageCount()) {
- str.append("<a href=\"#\" οnclick='document.forms(0).jumpPage.value="
- + p.getPageCount()
- + ";document.forms(0).submit();'>末页</a> ");
- } else {
- //str.append("<a href=\"#\" >末页</a> ");
- str.append("末页 ");
- }
- str.append(" 共" + p.getCount() + "条记录");
- str
- .append(" 每页<SELECT size=1 name=pagesize οnchange='this.form.jumpPage.value=1;this.form.pageSize.value=this.value;this.form.submit();'>");
- if (p.getPageSize() == 3) {
- str.append("<OPTION value=3 selected>3</OPTION>");
- } else {
- str.append("<OPTION value=3>3</OPTION>");
- }
- if (p.getPageSize() == 10) {
- str.append("<OPTION value=10 selected>10</OPTION>");
- } else {
- str.append("<OPTION value=10>10</OPTION>");
- }
- if (p.getPageSize() == 20) {
- str.append("<OPTION value=20 selected>20</OPTION>");
- } else {
- str.append("<OPTION value=20>20</OPTION>");
- }
- if (p.getPageSize() == 50) {
- str.append("<OPTION value=50 selected>50</OPTION>");
- } else {
- str.append("<OPTION value=50>50</OPTION>");
- }
- if (p.getPageSize() == 100) {
- str.append("<OPTION value=100 selected>100</OPTION>");
- } else {
- str.append("<OPTION value=100>100</OPTION>");
- }
- str.append("</SELECT>");
- str.append("条 分" + p.getPageCount() + "页显示 转到");
- str
- .append("<SELECT size=1 name=Pagelist οnchange='this.form.jumpPage.value=this.value;this.form.submit();'>");
- for (int i = 1; i < p.getPageCount() + 1; i++) {
- if (i == p.getPage()) {
- str.append("<OPTION value=" + i + " selected>" + i
- + "</OPTION>");
- } else {
- str.append("<OPTION value=" + i + ">" + i + "</OPTION>");
- }
- }
- str.append("</SELECT>页");
- str.append("<INPUT type=hidden value=" + p.getPage()
- + " name=\"pages\" > ");
- str.append("<INPUT type=hidden value=" + p.getPageSize()
- + " name=\"pageSize\"> ");
- return str.toString();
- }
- /**
- * 获取总条数
- */
- public Long getTotalCount(PageBean p) throws Exception {
- List list = getHibernateTemplate().find(p.getTotalCountSQL());
- long count = 0;
- if (list.size() > 0) {
- count = new Long(""+list.get(0));
- }
- return count;
- }
- /**
- * 查询信息进行分页
- */
- public List getList(final PageBean p) {
- return this.getHibernateTemplate().executeFind(new HibernateCallback(){
- public Object doInHibernate(Session session){
- Query q = session.createQuery(p.getListSQL());
- q.setFirstResult((p.getPage() - 1) * p.getPageSize());
- q.setMaxResults(p.getPageSize());
- return q.list();
- }
- });
- }
- /**
- * 查询信息进行分页 带有参数的
- */
- public List getList(final PageBean p,final String str[], final Object ob2[]) {
- return this.getHibernateTemplate().executeFind(new HibernateCallback(){
- public Object doInHibernate(Session session){
- Query q = session.createQuery(p.getListSQL());
- if(str!=null){
- for (int i = 0; i < str.length; i++) {
- q.setParameter(str[i], ob2[i]);
- }
- }
- q.setFirstResult((p.getPage() - 1) * p.getPageSize());
- q.setMaxResults(p.getPageSize());
- return q.list();
- }
- });
- }
- /**
- * 获取总条数 带有参数的
- */
- public Long getTotalCount(PageBean p, String str[], Object ob2[])throws Exception {
- List list=new ArrayList();
- if(str!=null && str.length>0){
- list = getHibernateTemplate().findByNamedParam(p.getTotalCountSQL(), str, ob2);
- }else{
- list=this.getHibernateTemplate().find(p.getTotalCountSQL());
- }
- long count = 0;
- if (list.size() > 0) {
- count = (Long)list.get(0);
- }
- return count;
- }
- }
Action的调用
- package com.yizhou.common.test;
- import java.util.List;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.http.HttpSession;
- import org.apache.struts.action.Action;
- import org.apache.struts.action.ActionForm;
- import org.apache.struts.action.ActionForward;
- import org.apache.struts.action.ActionMapping;
- /**
- * 分页代码示例 调用
- * Copyright @ 2008 NANJING YIZHOU SOFTWARE Co. Ltd.
- * All right reserved.
- *
- * @author xuxinlong
- *
- */
- public class SplitPageAction extends Action {
- private PaginateInterface pageinate;
- public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
- HttpSession session=request.getSession();
- Object obj=session.getAttribute("KDUser");
- if(obj!=null){
- PageBean pb=new PageBean();
- String jumpPage=request.getParameter("jumpPage");
- String pageSize=request.getParameter("pageSize");
- if(jumpPage!=null && !"".equals(jumpPage) && pageSize!=null && !"".equals(pageSize)){
- pb.setPageSize(new Integer(pageSize));
- }else{
- jumpPage="1";
- }
- String strSqlCnt="select count(*) from TUsertable";
- String strSqlInfo="select u from TUsertable u";
- pb.setTotalCountSQL(strSqlCnt);
- pb.setListSQL(strSqlInfo);
- pb.setPage(new Integer(jumpPage));
- pb.setCount(this.pageinate.getTotalCount(pb).intValue());
- List listUser=this.pageinate.getList(pb);
- request.setAttribute("info", this.pageinate.getToolsMenu(pb));
- request.setAttribute("listUser", listUser);
- return mapping.findForward("pagelist");
- }
- return mapping.findForward("error");
- }
- public PaginateInterface getPageinate() {
- return pageinate;
- }
- public void setPageinate(PaginateInterface pageinate) {
- this.pageinate = pageinate;
- }
- }
spring文件配置
- <bean id="pageinate" class="com.kingdo.common.test.Paginate">
- <property name="sessionFactory">
- <ref local="sessionFactory" />
- </property>
- </bean>
- <bean name="/splitPage" class="com.kingdo.common.test.SplitPageAction" singleton="false">
- <property name="pageinate">
- <ref bean="pageinate" />
- </property>
- </bean>
JSP中使用
- <%@ page language="java" pageEncoding="utf-8"%>
- <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
- <html>
- <head>
- <title>分页演示</title>
- <link href="css/list.css" rel="stylesheet" type="text/css" />
- <link href="css/table.css" rel="stylesheet" type="text/css" />
- </head>
- <body> <center><font size="5">分页演示</font></center>
- <form action="splitPage.do" method="post" name="splitPageForm">
- <input type="hidden" name="jumpPage">
- <table width="96%" class="center" border="0" cellpadding="0" cellspacing="0">
- <tr class="table_tr">
- <td >
- <table cla