Struts2+Spring+Hibernate分页查询


<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>百通网后台</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	
	<script type="text/javascript">
		function skip(){
			var currentPage=document.getElementById('textfield').value;
			document.location="admin/scanNews.action?currentPage="+currentPage;
		}
		function nextPage(){
			document.location="admin/scanNews.action?currentPage=<s:property value="%{pageBean.currentPage+1}"/>";
		}
		function prePage(){
			document.location="admin/scanNews.action?currentPage=<s:property value="%{pageBean.currentPage-1}"/>";
		}
	</script>
    <link href="http://192.168.26.195:8080/btw/css/body.css" rel="stylesheet" type="text/css">
	<link href="http://192.168.26.195:8080/btw/css/mTable.css" rel="stylesheet" type="text/css" />
  </head>
  
  <body>
  <center>
     <table border="0" cellspacing="0"  class="tables">
 		<tr>
 			<td align="center" class="t_mediumText">新闻ID</td>
 			<td align="center" class="t_mediumText">添加时间</td>
 	 		<td align="center" class="t_xlargeText">新闻标题</td>
 	 		<td align="center" class="t_mediumText">添加人</td>
 	 		<td align="center" class="t_mediumText">新闻类型</td>
 	 		<td align="center" class="t_smallText">编辑</td>
 	 		<td align="center" class="t_smallText">删除</td>
 	 	</tr>
 	 <s:iterator value="resultList" id="news">
 	 	<tr style="background-color:#FFFFFF">
 	 	<td align="center" class="mediumText" style="color:green"><s:property value="#news.id"/></td>
 	 		<td align="center" class="mediumText" style="color:green"><s:property value="#news.createdate"/></td>
 	 		<td align="center" class="xlargeText"><a href="admin/scanDetail!ForNews.action?id=${news.id}" class="tableChars"><s:property value="#news.title"/></a></td>
 	 		<td align="center" class="mediumText"><s:property value="#news.author"/></td>
			<td align="center" class="mediumText" style="color:green"><s:property value="#news.type"/></td>
			<td align="center" class="smallText">
				<a href="admin/editNews!getNewsDetail.action?id=${news.id}"><span class="tableChars">编辑</span></a>
			</td>
			<td align="center" class="smallText">
				<a onclick="{if(confirm('删除后不可恢复,确实要删除该信息?')){return true;}return false;}"
										href="admin/deleteNewsById.action?&id=${news.id}"><span class="tableChars">删除</span></a>
			</td>
 		</tr>
 	 </s:iterator>
 	 </table><br>
 		<span class="pageInfoChars">
 			共<span style="color:red"> ${recordSum} </span>条记录 <span style="color:red"> ${PageSum} </span>页
 			当前位于第<span style="color:red"> ${currentPage} </span>页
 			<a href="admin/scanNews.action?currentPage=1" class="pageInfoChars">首页</a> 
 			<input type="image" src="images/prePage.gif"  onclick="prePage()"/>
 			<input type="image" src="images/nextPage.gif" onclick="nextPage()"/>
 			<a href="admin/scanNews.action?currentPage=<s:property value="PageSum"/>" class="pageInfoChars">尾页</a>
 			<input name="currentPage" type="text" style="width: 25px;height: 20px;" id="textfield" />
 			<a href="javascript:skip()"  class="pageInfoChars">GO</a>
 		</span>
 	 </center>
  </body>
</html>

package com.baitw.utils.entity;

import java.util.List;

/**
 * 
 * 分页查询实体
 * 
 * */

public class PageBean {
	
	/*==============================================================*/
	private List resultList;//结果集
	private int recordSUM;//总记录数
	private int pageSUM;//总页数
	private int currentPage;//当前页
	private int pageSize;//页记录数
	private boolean isFirstPage;//是否是第一页   
	private boolean isLastPage;//是否是最后一页
	private boolean hasPreviousPage;//是否有上一页
	private boolean hasNextPage;//是否有下一页
	/*===========================初始化===================================*/
	public void init(){ 
		this.isFirstPage = isFirstPage(); 
	    this.isLastPage = isLastPage(); 
	    this.hasPreviousPage = isHasPreviousPage(); 
	    this.hasNextPage = isHasNextPage(); 
	} 
	/*=============================总页数=================================*/
	public static int countTotalPage(int pageSize,int recordSUM){ 
		
		int totalPage = recordSUM % pageSize == 0 ? recordSUM/pageSize : recordSUM/pageSize+1; 
		
	    return totalPage; 
	} 
	/*============================当前页开始记录号 ==================================*/
	public static int countOffset(final int pageSize,final int currentPage){ 
		
		final int offset = pageSize*(currentPage-1); 
		
	    return offset; 
	} 
	/*=============================当期页=================================*/
	public static int countCurrentPage(int page){ 
		
		final int curPage = (page==0?1:page); 
		
	    return curPage; 
	} 
	/*============================判断当前页的状态==================================*/
	public boolean isFirstPage() { 
		return currentPage == 1;    
	} 
	public boolean isLastPage() { 
	    return currentPage == pageSUM;    
	} 
	public boolean isHasPreviousPage() { 
	    return currentPage != 1;         
	} 
	public boolean isHasNextPage() { 
		return currentPage != pageSUM;    
	} 
	/*===========================Get/Set方法===================================*/
	public List getResultList() {
		return resultList;
	}
	public void setResultList(List resultList) {
		this.resultList = resultList;
	}
	public int getRecordSUM() {
		return recordSUM;
	}
	public void setRecordSUM(int recordSUM) {
		this.recordSUM = recordSUM;
	}
	public int getPageSUM() {
		return pageSUM;
	}
	public void setPageSUM(int pageSUM) {
		this.pageSUM = pageSUM;
	}
	public int getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	/*==============================================================*/
     
}

package com.baitw.service.impl;

import java.util.List;

import com.baitw.dao.PageDao;
import com.baitw.service.PageService;
import com.baitw.utils.entity.PageBean;

/**
 * 
 * 分页查询Service
 * 
 * */

public class PageServiceImpl implements PageService {

	public PageBean TabbedBrowsing(PageDao pageDao, int pageSize, int currentPage,
			String sql) {
		// TODO Auto-generated method stub
		int recordSum = pageDao.getAllRowCount(sql);//总记录数 
		int pageSum = PageBean.countTotalPage(pageSize, recordSum);//总页数 
		final int currentRecordIndex = PageBean.countOffset(pageSize, currentPage);//当前页开始记录 
		final int resultMaxSize = pageSize;//每页记录数 
		final int page = PageBean.countCurrentPage(currentPage);//当前页
		List list = pageDao.QueryForPage(sql, currentRecordIndex, resultMaxSize);//"一页"的记录 
	        
		//把分页信息保存到Bean中 
		PageBean pageBean = new PageBean(); 
		pageBean.setPageSize(pageSize);     
		pageBean.setCurrentPage(page); 
		pageBean.setRecordSUM(recordSum); 
		pageBean.setPageSUM(pageSum);
		pageBean.setResultList(list);
		pageBean.init(); 
		return pageBean; 
	}
}

//新闻分页查询
	public List QueryForNews(String hql, int currentRecordIndex, int resultMaxSize) {
		// TODO Auto-generated method stub
		List list=new ArrayList();
		SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
		Session session=HibernateSessionFactory.getSession();
		Query query=session.createQuery(hql);
		query.setFirstResult(currentRecordIndex);
		query.setMaxResults(resultMaxSize);
		list=query.list();
		List resultList=new ArrayList();
		for(Iterator iter=list.iterator();iter.hasNext();){
			TNews n=(TNews) iter.next();
			MNews m=new MNews();
			m.setId(n.getId()+"");
			m.setAuthor(n.getTUser().getFullname());
			m.setCreatedate(format.format(n.getCreatetime()));
			m.setState(n.getState().toString());
			m.setTitle(n.getTitle());
			m.setType(getNewsType(n.getType()));
			resultList.add(m);
		}
		session.close();
		return resultList;
	}

package com.baitw.struts.action;

import java.util.ArrayList;
import java.util.List;

import test.com.baitw.hibernate.CreateActivityForDB;
import test.com.baitw.hibernate.CreateNewsForDB;

import com.baitw.dao.PageDao;
import com.baitw.service.PageService;
import com.baitw.utils.entity.PageBean;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 
 * 新闻分页查询
 * 
 * */

public class ScanNews extends ActionSupport {

	//通过applicationContext.xml配置文件注入UserService的值 
	
	private PageService pageService;//PAGE服务类
	private int currentPage; //当第几页   
	private PageBean pageBean;//包含分布信息的bean    
	private PageDao pageDao;//用户PageDao实现	
	private String hql;//查询SQL
	private int pageSize;//每页记录数
	private List pageNumber;//分页页码
	private int recordSum;//总记录数
	private int PageSum;//总页数
	List resultList=new ArrayList();//查询结果
	
	public int getRecordSum() {
		return recordSum;
	}

	public void setRecordSum(int recordSum) {
		this.recordSum = recordSum;
	}

	public int getPageSum() {
		return PageSum;
	}

	public void setPageSum(int pageSum) {
		PageSum = pageSum;
	}
	
	public int getPageSize() {
		return pageSize;
	}

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

	public List getResultList() {
		return resultList;
	}

	public void setResultList(List resultList) {
		this.resultList = resultList;
	}

	public PageService getPageService() {
		return pageService;
	}

	public void setPageService(PageService pageService) {
		this.pageService = pageService;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}

	public PageDao getPageDao() {
		return pageDao;
	}

	public void setPageDao(PageDao pageDao) {
		this.pageDao = pageDao;
	}

	public String getHql() {
		return hql;
	}

	public void setHql(String hql) {
		this.hql = hql;
	}

	public List getPageNumber() {
		return pageNumber;
	}

	public void setPageNumber(List pageNumber) {
		this.pageNumber = pageNumber;
	}

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		
	    pageBean = pageService.TabbedBrowsing(pageDao, pageSize, currentPage, hql);
	    
	    resultList=pageDao.QueryForNews(hql, getFirstNum()-1, pageSize);
	    
	    pageNumber=pageDao.getPageNumber(hql, pageSize);
	    
	    recordSum=pageDao.getAllRowCount(hql);
	    
	    PageSum=pageNumber.size();
	    
	    return "news_List"; 

	}
	
	private int getFirstNum(){
		if(currentPage==1){
			return 1;
		}else{
			return (currentPage-1)*pageSize+1;
		}
	}
}

<!-- 新闻分页查询 -->
	<!-- =================================================== -->
	<bean id="scanNews" class="com.baitw.struts.action.ScanNews">
		<property name="pageService" ref="pageService"/>
		<property name="pageBean" ref="pageBean"/>
		<property name="pageDao" ref="pageDao"/>
		<property name="currentPage" value="1"/>
		<property name="pageSize" value="16"/>
		<property name="hql" value="from TNews t where t.state=1 order by t.id desc"/>
	</bean>
	<!-- =================================================== -->

猜你喜欢

转载自xiongjiajia.iteye.com/blog/1454218
今日推荐