OA项目 分页功能总结一 简单的分页(资料来源于传智播客汤阳光的视频教程)

1.在domain包中新建一个PageBean类

  其主要的属性有:

                                //已知

                               private int  currentPage //当前页

                               private int   pageSize     //每页数据条数,默认为10条

                              

                                //需要查询数据库的

                               private int  recodCount  //总记录数

                               private List recordList  //当前页所需要的数据列表

                              

                                   //计算

                               private int  pageCount  //总页面数

                               private int startPageIndex; // 页码显示的起始页

                               private  int endPageIndex;  //页码显示的结束页


                        public  PageBean(int currentPage,int pageSize,List recordList,int recordCount){

                            pageCount=(recordCount+pageSize-1)/pageSize;

                           if(pageCount<10){

                               startPageIndex=1;

                                endPageIndex=pageCount;

                           }

                           else{

                            startPageIndex=currentPage-4;

                            endPageIndex=currentPage+5;

                             if( startPageIndex<1){

                                       startPageIndex=1;

                                       endPageIndex=10;

                              }

                          if(endPageIndex>pageCount){endPageIndex=pageCount;startPageIndex=pageCount-9;}

                             }

}


生成相应的set和get方法,这里省略。。。。


以下代码段的pagebean类的内容是我做项目的代码,和上面不一定完全相同

package cn.itcast.oa.domain;

import java.util.List;

public class PageBean {
    //查询
	private int recordCount;  //总记录数
	private List recordList;  //本页数据列表
	
	//传递的参数
	private int currentPage;   //当前页
	private int pageSize;      //当前页记录数
	
	//计算
	private int pageCount;   //总页数
	private int beginPageIndex;//开始的页面索引
	private int endPageIndex;//结束的页面索引
	
	
	
	public PageBean(int currentPage, int pageSize, List recordList,int recordCount) {
		this.currentPage = currentPage;
		this.pageSize = pageSize;
		this.recordList=recordList;
		this.recordCount=recordCount;
		
		
		//计算
		pageCount=(recordCount+pageSize-1)/pageSize;
		if(pageCount<10){
			beginPageIndex=1;
			endPageIndex=pageCount;
		}
		else{
			beginPageIndex=currentPage-4;
			endPageIndex=currentPage+5;
			if(beginPageIndex<1){
				beginPageIndex=1;
				endPageIndex=10;
			}
			else if(endPageIndex>pageCount){
				endPageIndex=pageCount;
				beginPageIndex=pageCount-9;
			}
			
		}
		
		
	}
	public int getRecordCount() {
		return recordCount;
	}
	public void setRecordCount(int recordCount) {
		this.recordCount = recordCount;
	}
	public List getRecordList() {
		return recordList;
	}
	public void setRecordList(List recordList) {
		this.recordList = recordList;
	}
	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;
	}
	public int getPageCount() {
		return pageCount;
	}
	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}
	public int getBeginPageIndex() {
		return beginPageIndex;
	}
	public void setBeginPageIndex(int beginPageIndex) {
		this.beginPageIndex = beginPageIndex;
	}
	public int getEndPageIndex() {
		return endPageIndex;
	}
	public void setEndPageIndex(int endPageIndex) {
		this.endPageIndex = endPageIndex;
	}
	
	
	
}


2.在config包中新建一个Configuration类,用来设置和得到每页的记录数pageSize,默认是10条

Public Class Configuration(){

private static int pageSize;

static {
   pageSize=10;
}

public static int getPageSize(){
    return pageSize;
}

}


3.在相应的Action(如:UserAction)中,修改原先在list方法中的内容,改为:

private int pageNum;

生成相应的seter,geter方法,这里省略。。。

   PageBean pageBean=userService.getPageBean(pageNum,user);

  ActionContext.getContext.put("pageBean",pageBean);



4.在Service和ServiceImpl中生成以下方法并实现它:

  public  PageBean getPageBean(int pageNum){

     int currentPage=pageNum;

     int pageSize=Configuration.getPageSize();


    //查询

   List  recordList=getSession.createQuery(//

                         "from User u ")//

                           .setFirstResults((currentPage-1)*pageSize)//

                           .setMaxResults(pageSize)//

                            .list();

   int  recordCount=getSession.createQuery(//

                         "select count(*) from User u ").list();

      return new PageBean(currentPage,pageSize,recordList,recordCount);

}


5.在相应的页面中插入以下的名称:

 如

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>

<div id=PageSelectorBar>
	<div id=PageSelectorMemo>
		页次:${currentPage}/${pageCount}页  
		每页显示:${pageSize }条  
		总记录数:${recordCount }条
	</div>
	<div id=PageSelectorSelectorArea>
	
		<a href="javascript: gotoPage(1)" title="首页" style="cursor: hand;">
			<img src="${pageContext.request.contextPath}/style/blue/images/pageSelector/firstPage.png"/>
		</a>
		
		<s:iterator begin="%{beginPageIndex}" end="%{endPageIndex}" var="num">
			<s:if test="currentPage == #num"><%-- 当前页 --%>
				<span class="PageSelectorNum PageSelectorSelected">${num}</span>
			</s:if>
			<s:else><%-- 非当前页 --%>
				<span class="PageSelectorNum" style="cursor: hand;" onClick="gotoPage(${num});">${num}</span>
			</s:else>
		</s:iterator>
		
		<a href="javascript: gotoPage(${pageCount})" title="尾页" style="cursor: hand;">
			<img src="${pageContext.request.contextPath}/style/blue/images/pageSelector/lastPage.png"/>
		</a>
		
		转到:
		<select id="pn" οnchange="gotoPage(this.value)">
			<s:iterator begin="1" end="%{pageCount}" var="num">
				<option value="${num}">${num}</option>					
			</s:iterator>
		</select>		
		<script type="text/javascript">
			// 回显页码
			$("#pn").val(${currentPage});
		</script>
		 <script type="text/javascript">
				function gotoPage( pageNum ){
				//window.location.href = "forumAction_show.action?id=${id}&pageNum=" + pageNum;
				$(document.forms[0]).append("<input name='pageNum' type='hidden' value='"+pageNum+"' />");
				document.forms[0].pageNum.value=pageNum;
				document.forms[0].submit();
				}
				</script>
		
	</div>
</div>


<!-- <script type="text/javascript">
	function gotoPage( pageNum ){
		window.location.href = "forumAction_show.action?id=${id}&pageNum=" + pageNum;
		
		/* $(document.forms[0]).append("<input type='hidden' name='pageNum' value='" + pageNum + "'/>");
		document.forms[0].submit(); // 提交表单 */
	}
</script> -->

6.运行,简单的分页即可完成

由于这只是简单的分页功能,在之后我会持续更新分页功能的改进,以实现更加复杂的业务逻辑需求,敬请关注!!!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值