一、前端分页
1、将分页部分单独做成一个jsp
2、页面引用
3、后台分页对象Page.java
4、servlet
5、dao层我们将分页查询的数据放入page对象中返回即可;
在page.jsp中,我们动态获取页面请求,并设置到分页form的action属性中,这样,一个页面可以满足不同分页请求
1、将分页部分单独做成一个jsp
<%@page import="java.net.URLEncoder"%>
<%@page language="java" pageEncoding="utf-8"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<link type="text/css" rel="stylesheet" href="<c:url value='/styles/page.css'/>"/>
<%
//每页显示记录数
int pageSize = (Integer)request.getAttribute("pageSize");
//最多显示分页页数
int displayPageCount = (Integer)request.getAttribute("displayPageCount");
//当前页
int currentPage = (Integer)request.getAttribute("currentPage");
//记录总数
int count = (Integer)request.getAttribute("count");
//总页数
int pageCount = 0;
if(count % pageSize == 0){
pageCount = count / pageSize;
} else {
pageCount = count / pageSize + 1;
}
if(pageCount < currentPage){
currentPage = pageCount;
}
//起始页
int pageBegin = currentPage - (displayPageCount / 2);
if(pageBegin < 1){
pageBegin = 1;
}
//结束页
int pageEnd = currentPage + (displayPageCount / 2);
if(pageEnd > pageCount){
pageEnd = pageCount;
}
//关键字
String keyWord = request.getAttribute("keyWord") == null
? "" : (String)request.getAttribute("keyWord");
keyWord = URLEncoder.encode(keyWord,"utf-8");
String actionString = (String)request.getAttribute("actionString");
%>
<!-- 页面显示连接 -->
<a id="start_page" class="start"><spring:message code="message.page.start"/></a>
<a id="pre_page" class="pre"><spring:message code="message.page.prePage"/></a>
<span>
<%
for(int i = pageBegin;i <= pageEnd;i ++){
if(i != currentPage){
%>
<a href="javascript:goPage(<%=i%>);"><%=i%></a>
<%
} else {
%>
<a class="selected"><%=i%></a>
<%
}
}
%>
</span>
<a id="next_page" class="next"><spring:message code="message.page.nextPage"/></a>
<a id="end_page" class="end"><spring:message code="message.page.endPage"/></a>
<script type="text/javascript">
var pageForm = document.forms["pageForm"];
pageForm.setAttribute("action","<%=actionString%>");
function prePage(){
pageForm.currentPage.value = pageForm.currentPage.value - 1;
pageForm.submit();
}
function goPage(currentPage){
pageForm.currentPage.value = currentPage;
pageForm.submit();
}
function nextPage(){
pageForm.currentPage.value = parseInt(pageForm.currentPage.value) + 1;
pageForm.submit();
}
//控制链接和样式
if(<%=pageCount%> <= 1){
document.getElementById("page_div").style.display = "none";
}
if(<%=currentPage%> == 1 && <%=pageCount%> > 1){
document.getElementById("next_page").href = "javascript:nextPage()";
document.getElementById("end_page").href = "javascript:goPage(<%=pageCount%>)";
document.getElementById("page_div").className = "page";
}
if(<%=currentPage%> > 1 && <%=currentPage%> != <%=pageCount%>){
document.getElementById("pre_page").href = "javascript:prePage()";
document.getElementById("start_page").href = "javascript:goPage(1)";
document.getElementById("next_page").href = "javascript:nextPage()";
document.getElementById("end_page").href = "javascript:goPage(<%=pageCount%>)";
document.getElementById("page_div").className = "page";
}
if(<%=currentPage%> > 1 && <%=currentPage%> == <%=pageCount%>){
document.getElementById("pre_page").href = "javascript:prePage()";
document.getElementById("start_page").href = "javascript:goPage(1)";
document.getElementById("page_div").className = "page";
}
</script>
2、页面引用
<div id="page_div" class="page">
<form name="pageForm">
<input type="hidden" name="currentPage" value="${currentPage }">
<input type="hidden" name="keyWord" value="${keyWord }"/>
</form>
<%@include file="../common/page.jsp" %>
</div>
3、后台分页对象Page.java
package com.cpkf.notpad.vo;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import com.cpkf.notpad.commons.constants.PageConstants;
/**
* Filename: Page.java
* Description: 分页对象
* Company:
* @author: Jiang.hu
* @version: 1.0
* Create at: 2011-6-6 下午12:47:38
* modified:
*/
public class Page {
//每页显示记录数
private int pageSize;
//显示分页数
private int displayPageCount;
//当前页
private int currentPage;
//总记录数
private int count;
//关键字
private String keyWord;
//当前页记录集合
private List list = new ArrayList();
//请求路径
private String actionString;
public String getActionString() {
return actionString;
}
public void setActionString(String actionString) {
this.actionString = actionString;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getDisplayPageCount() {
return displayPageCount;
}
public void setDisplayPageCount(int displayPageCount) {
this.displayPageCount = displayPageCount;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public String getKeyWord() {
return keyWord;
}
public void setKeyWord(String keyWord) {
this.keyWord = keyWord;
}
/*
* method name : initPage
* description : 初始化page对象
* @author : Jiang.Hu
* @param : @param request
* @return : Page
* Create at : 2011-6-6 下午12:56:40
* modified :
*/
public static Page initPage(HttpServletRequest request){
int currentPage = request.getParameter("currentPage") == null
? 1 : Integer.parseInt(request.getParameter("currentPage"));
String keyWord = request.getParameter("keyWord") == null
? "" : request.getParameter("keyWord");
String actionString = request.getContextPath() + request.getServletPath();
Page page = new Page();
page.setPageSize(PageConstants.PAGE_SIZE);
page.setDisplayPageCount(PageConstants.DISPLAY_PAGE_COUNT);
page.setCurrentPage(currentPage);
page.setKeyWord(keyWord);
page.setActionString(actionString);
return page;
}
/*
* method name : setPage
* description : 设置page对象
* @author : Jiang.Hu
* @param : @param request
* @param : @param page
* @return : void
* Create at : 2011-6-6 下午12:56:57
* modified :
*/
public static void setPage(HttpServletRequest request,Page page){
request.setAttribute("pageSize", page.getPageSize());
request.setAttribute("displayPageCount", page.getDisplayPageCount());
request.setAttribute("currentPage", page.getCurrentPage());
request.setAttribute("count", page.getCount());
request.setAttribute("keyWord", page.getKeyWord());
request.setAttribute("actionString", page.getActionString());
}
}
4、servlet
@RequestMapping(value="/account/showUserList.do",method={RequestMethod.POST,RequestMethod.GET})
public String showUserList(HttpServletRequest request,HttpServletResponse response){
Page page = accountService.getAllAccountVosForPage(Page.initPage(request));
List<AccountVo> accountList = page.getList();
if(accountList != null){
request.setAttribute(AccountConstants.ACCOUNT_LIST, accountList);
}
Page.setPage(request, page);
return "account/accountList";
}
5、dao层我们将分页查询的数据放入page对象中返回即可;
在page.jsp中,我们动态获取页面请求,并设置到分页form的action属性中,这样,一个页面可以满足不同分页请求