`

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>
	<!-- =================================================== -->
分享到:
评论

相关推荐

    struts2+spring2+hibernate3注册查询搜索分页实例

    总的来说,这个"Struts2+Spring2+Hibernate3注册查询搜索分页实例"是一个很好的学习资源,涵盖了Java Web开发中的基础和核心部分。通过学习这个实例,开发者不仅可以掌握三大框架的基本用法,还能了解到如何将它们...

    struts2+spring+hibernate分页查询

    **Hibernate分页查询** Hibernate支持两种分页方式:Criteria API和HQL(Hibernate Query Language)。Criteria API可以通过DetachedCriteria对象创建查询条件,然后调用setFirstResult和setMaxResults方法实现分页...

    Struts2+Spring3+Hibernate3 用户管理系统实例源码

    本例主要是实现了struts2+spring3+hibernate3的 基本框架搭建的注册登录,以及用户增删改查,适于初学者学习。 包括:注册 登录功能 分页的实现 前端校验 验证码的实现 注册时有ajax 校验,登录时 后台从数据库...

    Struts2 + Spring2 + Hibernate3经典分页(包含java及jsp源码)

    ### Struts2 + Spring2 + Hibernate3 经典分页技术解析 #### 一、概述 在企业级应用开发中,分页显示是常见的需求之一。对于数据量较大的查询结果,采用分页的方式不仅能减轻服务器的压力,提高用户体验,还能更好...

    struts2 + spring 3 + hibernate3.3整合实现图书馆管理管理

    Struts2、Spring和Hibernate是Java Web开发中的三大框架,它们的整合应用广泛用于构建复杂的Web应用程序,如本例中的图书馆管理系统。这个系统实现了用户登录和注册功能,并且提供了对书籍表的操作,包括增、删、改...

    ext3+struts2+hibernate+spring的CRUD+分页

    "ext3+struts2+hibernate+spring的CRUD+分页"是一个典型的Java Web开发组合,用于实现全面的数据管理功能。这个组合充分利用了各个框架的优势,提供了一个强大的后端数据处理和前端展示的解决方案。 首先,EXT3是一...

    STRUTS2+HIBERNATE详细的分页实现代码详细的分页实现代码

    为了实现分页功能,我们需要在Struts2中定义Action类来接收用户的翻页请求,并通过Hibernate进行数据查询。具体步骤如下: 1. **定义PageTool类** 在Struts2中,我们通常会定义一个PageTool类来封装分页所需的信息...

    Struts2.1.8+Spring2.5.6+Hibernate3.3.2项目整合

    Struts2.1.8+Spring2.5.6+Hibernate3.3.2是经典的Java Web开发框架组合,常被称为SSH。这三个框架协同工作,为开发者提供了强大的模型-视图-控制器(MVC)架构支持,使得企业级应用的开发更加高效和模块化。 Struts...

    struts2 + spring + hibernate + easyui 整合用户信息管理小案例

    系统搭建使用struts2 + spring + hibernate + easyui 整合的, 功能包括: 分页查询,自定义页面数据显示量,用户名模糊搜索,创建时间段,更新时间段内用户信息搜索,双击行进行编辑,多行选中删除,右键菜单已经写好...

    Struts2+Spring+Hibernate+Ehcache+AJAX+JQuery+Oracle 框架集成用户登录注册Demo工程

    4.在服务端分页查询功能,优点:实时性:跳页才查询。数据量小:只加载当前页的记录进行显示。 5.单数据源配置(兼容Tomcat和Weblogic)。 6.Hibernate继承 HibernateDaoSupport。 7.Spring+Junit4单元测试,优点:...

    struts2+spring+hibernate分页显示

    struts2+spring+hibernate分页显示 分页显示一直是web开发中一大烦琐的难题,传统的网页设计只在一个JSP或者ASP页面中书写所有关于数据库操作的代码,那样做分页可能简单一点,但当把网站分层开发后,分页就比较困难...

    android访问struts2+spring+hibernate应用

    此压缩包含有两个工程,一个工程是struts2+spring2.5+hibernate3.3整合的服务器端技术(全注解)(云端),另一个工程是android的手机应用,包含对ssh整合的云端数据的访问,能够在android输入信息,将数据传递给...

    Struts2 + Spring 2.5 + Hibernate 3.3 整合(实际使用项目,version1)

    此项目整合了目前主流和最前源的web开发技术:采用ehcache实现二级缓存(包含查询缓存);用sf4j及logback(log4j的升级版)记录日志;proxool(据说是dbcp和c3p0三者中最优秀的)做连接池;使用jquery的ajax实现仿...

    struts2+spring+hibernate分页显示完整代码

    本篇文章将详细讲解如何在基于Struts2、Spring和Hibernate的项目中实现分页功能。 首先,我们从DAO层开始。在`MemberDao`接口中,我们定义了两个关键的方法,一个是用于分页查询,另一个是获取所有记录的数量。这两...

    struts2 + spring2 + hibernate3 .pdf

    根据提供的文件信息,本文将详细解析“Struts2 + Spring2 + Hibernate3”技术栈的集成与应用。本文主要从项目准备、环境搭建、公共类的设计等方面进行深入讲解。 ### 一、项目背景及意义 Struts2、Spring2 和 ...

    struts2+spring+hibernate分页实例

    这是一个struts2+hibernate+spring整合的实例,带有分页功能,希望对刚接触struts2的朋友有些帮助。spring2.0,hibernate3.1分页用的是自字义标签,不要少了这个包standard.jar

    struts+spring+hibernate通用分页方法

    struts+spring+hibernate通用分页方法.rar 博文链接:https://igogogo9.iteye.com/blog/97692

    struts2+spring+hibernate3的简易图书管理系统

    struts2+spring+hibernate3的简易图书管理系统,特别适合初学者集成! 实现了简单的分页和模糊查询。(基本上涵盖了开发ssh2的包),值得拥有哦! 注:数据库sql文件(mysql 5.5)在里面,要自己配好!

    Struts1.2+Spring2.5+Hibernate3.2+Jmesa2.4.3+Jmesatag实例2分页A

    一个Struts1.2+Spring2.5+Hibernate3.2+Jmesa2.4.3+JmesaTag实现的强大分页控件例子 里 面有action实现分页和标签实现分页2个例子 action实现的分页 包括了表头排序,导出为PDF,TXT, excel等格式 jar包和数据库...

Global site tag (gtag.js) - Google Analytics