S2SH通用分页

1.DTO

package com.lzh.lms.dto;

import java.util.List;

/**
 * @author luozehua
 * @date 2014年3月19日
 * @time 上午9:20:30
 */
public class PageBean<T> {
	/**
	 * 当前第几页
	 */
	private Integer pageNo;

	/**
	 * 每页显示记录数
	 */
	private Integer pageSize;

	private Integer totalRecords;

	private List<T> datas;

	public Integer getPageNo() {
		return pageNo;
	}

	public void setPageNo(Integer pageNo) {
		this.pageNo = pageNo;
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

	public Integer getTotalRecords() {
		return totalRecords;
	}

	public void setTotalRecords(Integer totalRecords) {
		this.totalRecords = totalRecords;
	}

	public List<T> getDatas() {
		return datas;
	}

	public void setDatas(List<T> datas) {
		this.datas = datas;
	}

	
	/**
	 * 首页
	 * 
	 * @return
	 */
	public Integer getFirstPage() {
		return 1;
	}

	/**
	 * 尾页
	 * 
	 * @return
	 */
	public Integer getLastPage() {
		return this.getTotalPages();
	}

	/**
	 * 上一页
	 * 
	 * @return
	 */
	public Integer getPreviousPage() {
		if (this.pageNo <= 1) {
			return 1;
		}
		return this.pageNo - 1;
	}

	/**
	 * 下一页
	 * 
	 * @return
	 */
	public Integer getNextPage() {
		if (this.pageNo >= this.getTotalPages()) {
			return this.getTotalPages();
		}
		return this.pageNo + 1;
	}

	/**
	 * 总页数
	 * 
	 * @return
	 */
	public Integer getTotalPages() {
		return (this.totalRecords + this.pageSize - 1) / this.pageSize;
	}
}

2.SystemContext

package com.lzh.lms.common;

/**
 * @author luozehua
 * @date 2014年3月19日
 * @time 下午1:53:12
 */
public class SystemContext {
	private static ThreadLocal<Integer> pageNoHolder = new ThreadLocal<Integer>();
	private static ThreadLocal<Integer> pageSizeHolder = new ThreadLocal<Integer>();

	public static Integer getPageNo() {
		if(pageNoHolder.get()==null){
			pageNoHolder.set(Constants.DEFAULT_PAGE_NO);
		}
		return pageNoHolder.get();
	}

	public static void setPageNo(Integer page_no) {
		if (page_no == null) {
			pageNoHolder.set(Constants.DEFAULT_PAGE_NO);
		} else {
			pageNoHolder.set(page_no);
		}
	}

	public static Integer getPageSize() {
		if(pageSizeHolder.get()==null){
			pageSizeHolder.set(Constants.DEFAULT_PAGE_SIZE);
		}
		return pageSizeHolder.get();
	}

	public static void setPageSizer(Integer page_size) {
		if (page_size == null) {
			pageSizeHolder.set(Constants.DEFAULT_PAGE_SIZE);
		} else {
			pageSizeHolder.set(page_size);
		}
	}

	public static void removePageNo() {
		pageNoHolder.remove();
	}

	public static void removePageSize() {
		pageSizeHolder.remove();
	}
}

3.Constants(初始常量,默认值)

package com.lzh.lms.common;

public class Constants {

	public static final Integer DEFAULT_PAGE_NO = 1;
	public static final Integer DEFAULT_PAGE_SIZE = 3;
}

4.DaoSupport俩方法

/**
	 * 分页
	 */
	@SuppressWarnings("unchecked")
	@Override
	public PageBean<T> searchPaginated(String whereHql,final Object[] params) {
		final Integer pageNo = SystemContext.getPageNo();
		final Integer pageSize = SystemContext.getPageSize();
		final StringBuffer hql = new StringBuffer(" from " + clazz.getSimpleName() + " o ");
		List<T> datas = Collections.EMPTY_LIST;

		PageBean<T> pageBean = new PageBean<T>();
		pageBean.setPageNo(pageNo);
		pageBean.setPageSize(pageSize);

		if (StringUtils.isNotBlank(whereHql)) {
			//有查询条件的情况下进行分页查询
			hql.append(whereHql);
			datas = this.hibernateTemplate.executeFind(new HibernateCallback() {
				@Override
				public Object doInHibernate(Session session) throws HibernateException, SQLException {
					Query query = session.createQuery(hql.toString()).setFirstResult((pageNo - 1) * pageSize).setMaxResults(pageSize);
					for (int i = 0; i < params.length; i++) {
						query.setParameter(i, params[i]);
					}
					return query.list();
				}
			});
		}else{
			datas = this.hibernateTemplate.executeFind(new HibernateCallback() {
				@Override
				public Object doInHibernate(Session session) throws HibernateException, SQLException {
					//链式编程
					return session.createQuery(hql.toString()).setFirstResult((pageNo-1)*pageSize).setMaxResults(pageSize).list();
				}
			});
		}
		pageBean.setDatas(datas);
		pageBean.setTotalRecords(getTotalRecords(whereHql,params));
		return pageBean;
	}

	/**查询总记录数
	 * @param whereHql
	 * @param params
	 * @return
	 */
	private Integer getTotalRecords(String whereHql, final Object[] params) {
		final StringBuffer hql = new StringBuffer("select count(*) from " + clazz.getSimpleName()+" o ");
		Long count = null;
		if(StringUtils.isNotBlank(whereHql)){
			hql.append(whereHql);
			count = (Long) this.hibernateTemplate.execute(new HibernateCallback() {
				@Override
				public Object doInHibernate(Session session) throws HibernateException, SQLException {
					Query query = session.createQuery(hql.toString());
					for (int i = 0; i < params.length; i++) {
						query.setParameter(i, params[i]);
					}
					return query.uniqueResult();
				}
			});
		}else{
			count = (Long) this.hibernateTemplate.execute(new HibernateCallback() {
				@Override
				public Object doInHibernate(Session session) throws HibernateException, SQLException {
					return session.createQuery(hql.toString()).uniqueResult();
				}
			});
		}
		return count.intValue();
	}

5.Action

	// 显示电脑信息并分页
		public String listAllComputersAndPages() throws Exception {
			PageBean<Computer> pegeBean =this.computerService.searchComputerPaginated(null, null);
			ActionContext.getContext().put("pageBean", pegeBean);
			return "page_success";
		}

6.jsp中

 <tr>
    <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="8" background="${pageContext.request.contextPath }/images2/tab_12.gif"> </td>
        <td><table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="b5d6e6" >
          <tr>
            <td width="10%" height="22" background="${pageContext.request.contextPath }/images2/bg.gif" bgcolor="#FFFFFF"><div align="center"><span class="STYLE1">计算机编号</span></div></td>
            <td width="12%" height="22" background="${pageContext.request.contextPath }/images2/bg.gif" bgcolor="#FFFFFF"><div align="center"><span class="STYLE1">所在机房</span></div></td>
            <td width="14%" height="22" background="${pageContext.request.contextPath }/images2/bg.gif" bgcolor="#FFFFFF"><div align="center"><span class="STYLE1">计算机型号</span></div></td>
            <td width="18%" background="${pageContext.request.contextPath }/images2/bg.gif" bgcolor="#FFFFFF"><div align="center"><span class="STYLE1">购买时间</span></div></td>
            <td width="23%" height="22" background="${pageContext.request.contextPath }/images2/bg.gif" bgcolor="#FFFFFF"><div align="center"><span class="STYLE1">使用状态</span></div></td>
            <td width="15%" height="22" background="${pageContext.request.contextPath }/images2/bg.gif" bgcolor="#FFFFFF" class="STYLE1"><div align="center">基本操作</div></td>
          </tr>
          
          <c:if test="${empty pageBean.datas}">
            <tr>
            	<td colspan="7"><font color="blue">暂时没有计算机管理数据……</font></td>
            </tr>
          </c:if>
         
         <c:forEach items="${pageBean.datas}" var="computer">
         <tr>
            <td height="20" bgcolor="#FFFFFF"><div align="center" class="STYLE1">
              <div align="center">${ computer.computerId}</div>
            </div></td>
            <td height="20" bgcolor="#FFFFFF"><div align="center"><span class="STYLE1">${computer.location }</span></div></td>
            <td height="20" bgcolor="#FFFFFF"><div align="center"><span class="STYLE1">${computer.computerType }</span></div></td>
            <td bgcolor="#FFFFFF"><div align="center"><span class="STYLE1"><fmt:formatDate value="${computer.buyTime}" pattern="yyyy-MM-dd"/></span></div></td>
            <td height="20" bgcolor="#FFFFFF"><div align="center"><span class="STYLE1">${computer.state}</span></div></td>
            <td height="20" bgcolor="#FFFFFF"><div align="center">
            <span class="STYLE4"><img src="${pageContext.request.contextPath }/images2/edt.gif" width="16" height="16" />
            <a href="${pageContext.request.contextPath }/equipment/computer!updateComputerUI?computerId=${computer.id}">编辑</a>   <img src="${pageContext.request.contextPath }/images2/del.gif" width="16" height="16" />
            <a href="${pageContext.request.contextPath }/equipment/computer!deleteComputer?computerId=${computer.id}">删除</a></span></div></td>
          </tr>
         </c:forEach>
         
         
        </table></td>
        <td width="8" background="${pageContext.request.contextPath }/images2/tab_15.gif"> </td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td height="35" background="${pageContext.request.contextPath }/images2/tab_19.gif"><table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="12" height="35"><img src="${pageContext.request.contextPath }/images2/tab_18.gif" width="12" height="35" /></td>
        <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td class="STYLE4">  共有 ${pageBean.totalRecords } 条记录,当前第 ${pageBean.pageNo}/${pageBean.totalPages } 页</td>
            <td><table border="0" align="right" cellpadding="0" cellspacing="0">
                <tr>
                  <!-- 首页 -->
                  <td width="40"><a href="${pageContext.request.contextPath }/equipment/computer!listAllComputersAndPages?pageNo=${pageBean.firstPage}&pageSize=${pageBean.pageSize}" ><img src="${pageContext.request.contextPath }/images2/first.gif" width="37" height="15" /></a></td>
                  <!-- 上一页 -->
                  <td width="45"><a href="${pageContext.request.contextPath }/equipment/computer!listAllComputersAndPages?pageNo=${pageBean.previousPage}&pageSize=${pageBean.pageSize}" ><img src="${pageContext.request.contextPath }/images2/back.gif" width="43" height="15" /></a></td>
                  <!-- 下一页 -->
                  <td width="45"><a href="${pageContext.request.contextPath }/equipment/computer!listAllComputersAndPages?pageNo=${pageBean.nextPage}&pageSize=${pageBean.pageSize}" target="Tag"><img src="${pageContext.request.contextPath }/images2/next.gif" width="43" height="15" /></a></td>
                  <!-- 尾页 -->
                  <td width="40"><a href="${pageContext.request.contextPath }/equipment/computer!listAllComputersAndPages?pageNo=${pageBean.lastPage}&pageSize=${pageBean.pageSize}"><img src="${pageContext.request.contextPath }/images2/last.gif" width="37" height="15" /></a></td>
                  <td width="100"><div align="center"><span class="STYLE1">转到第
                    <input name="textfield" type="text" size="4" style="height:12px; width:20px; border:1px solid #999999;" /> 
                    页 </span></div></td>
                  <td width="40"><img src="${pageContext.request.contextPath }/images2/go.gif" width="37" height="15" /></td>
                </tr>
            </table></td>
          </tr>
        </table></td>
        <td width="16"><img src="${pageContext.request.contextPath }/images2/tab_20.gif" width="16" height="35" /></td>
      </tr>

7.Struts配置文件

<action name="computer" class="computerAction">
			<result name="page_success">/jsp/equipment/computer.jsp</result>
			<result name="add_success" type="redirectAction">
				<param name="actionName">computer</param>
				<param name="method">listAllComputersAndPages</param>
			</result>
			<result name="updateUI">/jsp/equipment/updateComputer.jsp</result>
			<result name="update_success" type="redirectAction">
				<param name="actionName">computer</param>
				<param name="method">listAllComputersAndPages</param>
			</result>

			<result name="delete_success" type="redirectAction">
				<param name="actionName">computer</param>
				<param name="method">listAllComputersAndPages</param>
			</result>
		</action>





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值