分页处理方法介绍

第一种是没有参数的查询分页处理
后台代码如下:

public ActionForward execute(ActionMapping mapping ,ActionForm actionForm,HttpServletRequest request,HttpServletResponse response)throws Exception {
 int pageSize = 3; //定义每页显示记录数量
 int currentPage; //定义当前页
 String pageNoStr =request.getParameter("page"); //获取当前页码
if(pageNoStr == null || pageNoStr.trim().equals("")){
    currentPage = 1;
}
try{
	currentPage = Integer.parseInt(pageNoStr.trim());
   }
catch(Exception e){
	currentPage = 1;
   }
   List<Yctsjl> list = yctsjlService.findByParameter(currentPage,pageSize); //业务逻辑 ,实现分页
   int count = yctsjlService.getCount(bdzmc, beginDate, endDate); //总的记录条数
   int pageCount =yctsjlService.getPageCount(count,pageSize);//获取总页数
	request.setAttribute("yctsjlList", list);
	request.setAttribute("currentPage",currentPage);
	request.setAttribute("count",count);
	request.setAttribute("pageCount" ,pageCount);
	return mapping.findForward("list");
}

前台代码如下
list.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>登录页面</title>
<link rel="stylesheet" href="<%=request.getContextPath() %>/css/base1.css" type="text/css" >
</head>
<body>
<br>
<table width="470" align="center">
	<tr align="center" class="style6">
	 <td>
		总共<font color="red"><bean:write name="count"/></font>条记录 
		第<font color="red"><bean:write name="currentPage" /></font>页  
		共<font color="red"><bean:write name="pageCount" /></font>页
    </td>
<%	if(((Integer)request.getAttribute("pageCount")==1)||((Integer)request.getAttribute("currentPage")==1)){%>
<td align="center" >首页</td>
<td valign="top"><br></td>
<td valign="top"><br></td><td align="center">上一页</td>
<%}else{ %>	
<td align="center">
	<a href="<%=request.getContextPath() %>/jsp/xtwh/list.do?action=listUser&page=1">首页</a>
</td>
<td valign="top"><br></td>
<td valign="top"><br></td>
<td align="center">
	<a href="<%=request.getContextPath() %>/jsp/xtwh/list.do?action=listUser&page=<%=(Integer)request.getAttribute("currentPage")-1 %>">上一页</a>
</td>
<%} %>
<%	if((Integer)request.getAttribute("pagesNum")==1||((Integer)request.getAttribute("currentPage")>=(Integer)request.getAttribute("countPage"))){%>
<td align="center">下一页</td>
<td valign="top"><br></td><td valign="top"><br></td>
<td align="center">尾页</td>
<%}else{ %>
<td align="center">
	<a href="<%=request.getContextPath() %>/jsp/xtwh/list.do?page=<%=(Integer)request.getAttribute("currentPage")+1%>">下一页</a>
</td>
<td align="center">
<a href="<%=request.getContextPath() %>/jsp/xtwh/list.do?pageNum=<%=(Integer)request.getAttribute("pagesNum") %>">尾页</a>
</td>
  <%} %>
<tr>
</table>
</body>
</html>


另一种情况是在查询时有参数的分页处理
本人使用的是Hibernate+struts+Map技术来处理带N个参数的查询
利用struts里面的<html:link name="map" paramId="参数名" paramName="变量名" paramProperty="变量属性"></html:link>
通过这个可以传递N个参数,
基础类如下:
package cn.com.model;

import java.util.Date;

/**
 * Yctsjl generated by MyEclipse Persistence Tools
 */

public class Yctsjl implements java.io.Serializable {

	// Fields

	private Long id;
	private Long mxdydj;
	private Long bdzid;
	private String bphyy;
	private String sfzcyc;
	private String ycsfxc;
	private Date ycfsrq;
	private Date ycxcrq;
	private String bz1;
	private String bz2;
	private String bz3;
	private Long hzjlid;
	private String bdzmc;

	// Constructors

	/** default constructor */
	public Yctsjl() {
	}

	/** full constructor */
	public Yctsjl(Long mxdydj, Long bdzid, String bphyy, String sfzcyc,
			String ycsfxc, Date ycfsrq, Date ycxcrq, String bz1, String bz2,
			String bz3, Long hzjlid, String bdzmc) {
		this.mxdydj = mxdydj;
		this.bdzid = bdzid;
		this.bphyy = bphyy;
		this.sfzcyc = sfzcyc;
		this.ycsfxc = ycsfxc;
		this.ycfsrq = ycfsrq;
		this.ycxcrq = ycxcrq;
		this.bz1 = bz1;
		this.bz2 = bz2;
		this.bz3 = bz3;
		this.hzjlid = hzjlid;
		this.bdzmc = bdzmc;
	}
	
	// Property accessors

	public Yctsjl(Long mxdydj, Long bdzid, String bphyy) {
		super();
		this.mxdydj = mxdydj;
		this.bdzid = bdzid;
		this.bphyy = bphyy;
	}

	public Long getId() {
		return this.id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long getMxdydj() {
		return this.mxdydj;
	}

	public void setMxdydj(Long mxdydj) {
		this.mxdydj = mxdydj;
	}

	public Long getBdzid() {
		return this.bdzid;
	}

	public void setBdzid(Long bdzid) {
		this.bdzid = bdzid;
	}

	public String getBphyy() {
		return this.bphyy;
	}

	public void setBphyy(String bphyy) {
		this.bphyy = bphyy;
	}

	public String getSfzcyc() {
		return this.sfzcyc;
	}

	public void setSfzcyc(String sfzcyc) {
		this.sfzcyc = sfzcyc;
	}

	public String getYcsfxc() {
		return this.ycsfxc;
	}

	public void setYcsfxc(String ycsfxc) {
		this.ycsfxc = ycsfxc;
	}

	public Date getYcfsrq() {
		return this.ycfsrq;
	}

	public void setYcfsrq(Date ycfsrq) {
		this.ycfsrq = ycfsrq;
	}

	public Date getYcxcrq() {
		return this.ycxcrq;
	}

	public void setYcxcrq(Date ycxcrq) {
		this.ycxcrq = ycxcrq;
	}

	public String getBz1() {
		return this.bz1;
	}

	public void setBz1(String bz1) {
		this.bz1 = bz1;
	}

	public String getBz2() {
		return this.bz2;
	}

	public void setBz2(String bz2) {
		this.bz2 = bz2;
	}

	public String getBz3() {
		return this.bz3;
	}

	public void setBz3(String bz3) {
		this.bz3 = bz3;
	}

	public Long getHzjlid() {
		return this.hzjlid;
	}

	public void setHzjlid(Long hzjlid) {
		this.hzjlid = hzjlid;
	}

	public String getBdzmc() {
		return this.bdzmc;
	}

	public void setBdzmc(String bdzmc) {
		this.bdzmc = bdzmc;
	}

}

映射文件如下
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="cn.com.model.Yctsjl" table="YCTSJL" schema="MXDLPH">
        <id name="id" type="java.lang.Long">
            <column name="ID" precision="22" scale="0" />
            <generator class="sequence" />
        </id>
        <property name="mxdydj" type="java.lang.Long">
            <column name="MXDYDJ" precision="22" scale="0" />
        </property>
        <property name="bdzid" type="java.lang.Long">
            <column name="BDZID" precision="22" scale="0" />
        </property>
        <property name="bphyy" type="java.lang.String">
            <column name="BPHYY" length="30" />
        </property>
        <property name="sfzcyc" type="java.lang.String">
            <column name="SFZCYC" length="1" />
        </property>
        <property name="ycsfxc" type="java.lang.String">
            <column name="YCSFXC" length="1" />
        </property>
        <property name="ycfsrq" type="java.util.Date">
            <column name="YCFSRQ" length="7" />
        </property>
        <property name="ycxcrq" type="java.util.Date">
            <column name="YCXCRQ" length="7" />
        </property>
        <property name="bz1" type="java.lang.String">
            <column name="BZ1" length="30" />
        </property>
        <property name="bz2" type="java.lang.String">
            <column name="BZ2" length="30" />
        </property>
        <property name="bz3" type="java.lang.String">
            <column name="BZ3" length="30" />
        </property>
        <property name="hzjlid" type="java.lang.Long">
            <column name="HZJLID" precision="22" scale="0" />
        </property>
        <property name="bdzmc" type="java.lang.String">
            <column name="BDZMC" />
        </property>
    </class>
</hibernate-mapping>


Action类如下:
public class YctsjlAction extends BaseAction {
	
	private YctsjlService yctsjlService;
	
	public YctsjlService getYctsjl(){
		return (YctsjlService) getWebApplicationContext().getBean("yctsjlService");
	}
	//返回所有的异常提示记录
	public ActionForward yctsjlList(ActionMapping mapping ,ActionForm actionForm
			,HttpServletRequest request,HttpServletResponse response)throws Exception {
		LazyValidatorForm form = (LazyValidatorForm) actionForm;
		yctsjlService = getYctsjl();		
		String bdzmc =(String )form.get("bdzmc");
		String beginDate = (String)form.get("beginDate");
		String endDate= (String)form.get("endDate");
		System.out.println("endDate"+endDate);
		if(bdzmc!=null){
			bdzmc = bdzmc.trim();
		}
		else {
			bdzmc = "";
		}
		if(beginDate!=null) beginDate = beginDate.trim();
		else beginDate = "";
		if(endDate!=null) endDate = endDate.trim();
		else endDate = "";
		
		//分页处理
		int pageSize = 3; //定义每页显示记录数量
		int currentPage; //定义当前页
		String pageNoStr =request.getParameter("page"); //获取当前页码
		if(pageNoStr == null || pageNoStr.trim().equals("")){
			currentPage = 1;
		}
		try{
			currentPage = Integer.parseInt(pageNoStr.trim());
		}
		catch(Exception e){
			currentPage = 1;
		}
		List<Yctsjl> list = yctsjlService.findByParameter(bdzmc,beginDate,endDate,currentPage,pageSize);
		int count = yctsjlService.getCount(bdzmc, beginDate, endDate); //总的记录条数
		int pageCount =yctsjlService.getPageCount(count,pageSize);//获取总页数
		
		PageUtils pageUtil = new PageUtils(currentPage, pageSize, count);
		request.setAttribute("pageUtil", pageUtil);
		Map pageParmMap = form.getMap();
		pageParmMap.remove("page"); 去除上一次的页码值
		Map map = new HashMap();
		map.putAll(pageParmMap);
		request.setAttribute("pageParmMap", map);
		request.setAttribute("pageUtil", pageUtil);
		request.setAttribute("yctsjlList", list);
		return mapping.findForward("list");
	}
}

前台页面 list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<%@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean" %>
<%@ taglib prefix="logic" uri="http://struts.apache.org/tags-logic" %>
<%@ taglib prefix="html" uri="http://struts.apache.org/tags-html"  %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>异常提示记录查询页面</title>
<link rel="stylesheet" href="<%=request.getContextPath() %>/css/base1.css" type="text/css" >
<script src="<%=request.getContextPath() %>/js/jquery-1.3.2.js" type="text/javascript"></script>
<script language="JavaScript" type="text/javascript"
			src="<%=request.getContextPath()%>/js/calendar/calendar.js"></script>
<script language="JavaScript" type="text/javascript"
			src="<%=request.getContextPath()%>/js/calendar/lang/calendar-zh.js"></script>
<script language="JavaScript" type="text/javascript"
			src="<%=request.getContextPath()%>/js/calendar/showCalendar.js "></script>
<link rel="stylesheet"  href="<%=request.getContextPath()%>/js/calendar/css/calendar-blue.css"
	type="text/css" />
	
</head>

<body>
<h3>异常提示记录</h3>
<html:form action="/jsp/tbdlgl/yctsjl.do?dispatch=yctsjlList "  method="post">
<table  border="0" cellpadding="0" cellspacing="0" width = "800">
	<tr class="style6">
		<td width="30%">
			变电站名称:<input type="text" id="bdzmc" name="bdzmc" value="<bean:write name="pageParmMap" property="bdzmc"/>">
		</td>
		<td  width="20%" align="center">发生日期:
			<input type="text" name="beginDate" id="beginDate" value="<bean:write name="pageParmMap" property="beginDate"/>" size="10">
		</td>
		<td  width="20%" align="center">消除日期:
			<input type="text" name="endDate" id="endDate" value="<bean:write name="pageParmMap" property="endDate"/>" size="10">
		</td>
		<td width="20%" align="center"><input type="submit" name="query" value="查询" ></td>
	</tr>
</table>

<br>
<table  border="1"   cellpadding="1" cellspacing="0" width="800" >
	<tr class="style7" align="center">
		<td width="7%" >变电站编号</td>
		<td width="20%">变电站名称</td>
		<td width="12%">母线电压等级</td>
		<td width="10%">异常种类</td>
		<td width="11%">异常消除情况</td>
		<td width="22%">异常发生日期</td>
		<td width="22%">异常消除日期</td>
	</tr>
	<logic:empty name="yctsjlList">
		目前没有信息
	</logic:empty>
	<logic:notEmpty name="yctsjlList">
		<logic:iterate  id="yctsjl" name="yctsjlList" type="cn.com.model.Yctsjl">
			<tr class="style7" align="center">
				<td><bean:write name="yctsjl" property="bdzid"/>&nbsp;</td>
				<td><bean:write name="yctsjl" property="bdzmc"/>&nbsp;</td>
				<td><bean:write name="yctsjl" property="mxdydj"/>&nbsp;</td>
				<logic:equal value="" name="yctsjl" property="sfzcyc">
					<td>没有记录</td>
				</logic:equal>
				<logic:equal value="0" name="yctsjl" property="sfzcyc">
					<td>非正常异常&nbsp;</td>
				</logic:equal>
				<logic:equal value="1" name="yctsjl" property="sfzcyc">
					<td>是正常异常&nbsp;</td>
				</logic:equal>
				<logic:equal value="" name="yctsjl" property="ycsfxc">
					<td>没有记录</td>
				</logic:equal>
				<logic:equal value="0" name="yctsjl" property="ycsfxc">
					<td><font color="red">异常未消除</font></td>
				</logic:equal>
				<logic:equal value="1" name="yctsjl" property="ycsfxc">
					<td>异常已消除</td>
				</logic:equal>
				<td><bean:write name="yctsjl" property="ycfsrq" format = "yyyy-MM-dd a HH:mm:ss " /> &nbsp;</td>
				<td><bean:write name="yctsjl" property="ycxcrq" format = "yyyy-MM-dd a HH:mm:ss "/>&nbsp;</td>
			</tr>
		</logic:iterate>
	</logic:notEmpty>
</table>
<br>
<table width="470" align="center">
	<tr align="center" class="style6">
	 <td>
		总共<font color="red"><bean:write name="pageUtil" property="count" /></font>条记录 
		第<font color="red"><bean:write name="pageUtil" property="page" /></font>页  
		共<font color="red"><bean:write name="pageUtil" property="pageCount" /></font>页
    </td>
    	<td align="center">
    		<html:link action="/jsp/tbdlgl/yctsjl.do?dispatch=yctsjlList" name ="pageParmMap" paramId="page" paramName="pageUtil" paramProperty="first">首页</html:link>
    	</td>
    	<td align="center">
    		<html:link action="/jsp/tbdlgl/yctsjl.do?dispatch=yctsjlList" name ="pageParmMap" paramId="page" paramName="pageUtil" paramProperty="previous">上一页</html:link>
    	</td>
 		<td align="center">
 			<html:link action="/jsp/tbdlgl/yctsjl.do?dispatch=yctsjlList" name ="pageParmMap" paramId="page" paramName="pageUtil" paramProperty="next">下一页</html:link>
 		</td>
 		<td align="center">
 			<html:link action="/jsp/tbdlgl/yctsjl.do?dispatch=yctsjlList" name ="pageParmMap" paramId="page" paramName="pageUtil" paramProperty="last">尾页</html:link>
 		</td>
 </tr>
 </table>
 </html:form>		
 <script language="JavaScript" type="text/javascript">
	var beginDate = document.getElementById('beginDate');
		beginDate.onfocus = function() {
			return showCalendar(beginDate, '%Y-%m-%d', '24', true);
		}
	var endDate = document.getElementById('endDate');
	    endDate.onfocus = function(){
	    	return showCalendar(endDate, '%Y-%m-%d', '24', true);
	    }
	    

  </script>
</body>
</html>


分页处理工具:
package cn.com.utility;



/**
 * @author wl
 *
 */
public class PageUtils {
	
	private int offSet = 0;
	
	private int page = 1;
	
	private int num = 1;
	
	private int count = 0;
	
	private int pageCount = 1;
	
	private final int first = 1;
	
	private int previous;
	
	private int next;
	
	private int last;
	
	/**
	 * @param page
	 * @param num
	 * @param orderBy
	 * @param asc
	 * @param criteria
	 */
	public PageUtils(int page, int num, int count) {
		this.num = num>1?num:1;
		this.count = count;
		this.pageCount = new Double(Math.ceil(new Float(this.count).floatValue()/new Float(this.num).floatValue())).intValue();
		this.pageCount = this.pageCount>1?this.pageCount:1;
		this.page = page>1?page:1;
		this.page = this.page>pageCount?pageCount:this.page;
		this.offSet = this.page*this.num-this.num;
		this.previous = this.page>1?this.page-1:1;
		this.next = this.page<this.pageCount?this.page+1:this.pageCount;
		this.last = this.pageCount;
	}
	
	/**
	 * @return Returns the count.
	 */
	public int getCount() {
		return count;
	}
	/**
	 * @return Returns the first.
	 */
	public int getFirst() {
		return first;
	}
	/**
	 * @return Returns the last.
	 */
	public int getLast() {
		return last;
	}
	/**
	 * @return Returns the next.
	 */
	public int getNext() {
		return next;
	}
	/**
	 * @return Returns the num.
	 */
	public int getNum() {
		return num;
	}
	/**
	 * @return Returns the offSet.
	 */
	public int getOffSet() {
		return offSet;
	}
	/**
	 * @return Returns the page.
	 */
	public int getPage() {
		return page;
	}
	/**
	 * @return Returns the pageCount.
	 */
	public int getPageCount() {
		return pageCount;
	}
	/**
	 * @return Returns the previous.
	 */
	public int getPrevious() {
		return previous;
	}
}



本人也是菜鸟希望多多指教,或有什么好的见解,完全可以发表评论。

你可能感兴趣的:(Hibernate,jsp,bean,struts,css)