1. 程式人生 > >jsp實現簡單分頁功能

jsp實現簡單分頁功能

一.webcontend層
1.webcontent/common/meta.jsp

程式碼:
<base href="${pageContext.request.scheme}://${pageContext.request.serverName}:${pageContext.request.serverPort}${pageContext.request.contextPath}/">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>

2.webcontent/page/page.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="/WEB-INF/c.tld" prefix="c"%>
<c:set var="page" value="${sessionScope.page}" />

<c:set var="path" value="${pageContext.request.contextPath}" />
<c:set var="url" value="${param.url}" />
<c:set var="urlParams" value="${param.urlParams}" />
<c:set var="pathurl" value="${path}/${url}" />
<tr align="center">
共${page.totalCount}條記錄 共${page.totalPage}頁 每頁顯示${page.everyPage}條

當前第${page.currentPage}頁
<c:choose>
<c:when test="${page.hasPrePage eq false}">
<<首頁 <上頁
</c:when>
<c:otherwise>
<a href="${pathurl}?&pageAction=first${urlParams}"><<首頁 </a>
<a href="${pathurl}?pageAction=previous${urlParams}" /><上一頁</a>
</c:otherwise>
</c:choose>
||
<c:choose>
<c:when test="${page.hasNextPage eq false}">
下頁> 尾頁>>
</c:when>
<c:otherwise>
<a href="${pathurl}?&pageAction=next${urlParams}">下一頁> </a>
<a href="${pathurl}?pageAction=last${urlParams}" />末頁>></a>
</c:otherwise>
</c:choose>

<SELECT name="indexChange" id="indexChange"
onchange="getCurrentPage(this.value);">
<c:forEach var="index" begin="1" end="${page.totalPage}" step="1">
<option value="${index}" ${page.currentPage eq index ? "selected" : ""}>
第${index}頁
</option>
</c:forEach>
</SELECT>

每頁顯示:<select name="everyPage" id="everyPage" onchange="setEveryPage(this.value);">
<c:forEach var="pageCount" begin="5" end="${page.totalCount}" step="5">
<option value="${pageCount}" ${page.everyPage eq pageCount ? "selected" : ""}>
${pageCount}條
</option>
</c:forEach>
</select>
</tr>
<div style='display: none'>
<a class=listlink id="indexPageHref" href='#'></a>
</div>
<script>
function getCurrentPage(index){
var a = document.getElementById("indexPageHref");
a.href = '${pathurl}?pageAction=gopage&pageKey='+index+'${urlParams}';
a.setAttribute("onclick",'');
a.click("return false");
}
function setEveryPage(everyPage){
var a = document.getElementById("indexPageHref");
var currentPage = document.getElementById('indexChange').value;
a.href = '${pathurl}?pageAction=setpage&pageKey='+everyPage+'${urlParams}';
a.setAttribute("onclick",'');
a.click("return false");
}
function sortPage(sortName){
var a = document.getElementById("indexPageHref");
a.href = '${pathurl}?pageAction=sort&pageKey='+sortName+'${urlParams}';
a.setAttribute("onclick",'');
a.click("return false");
}
</script>

二.java原始碼曾
3.controller 裡

程式碼:

@RequestMapping(value="xxxxx.htm",method=RequestMethod.GET)
public ModelAndView list(HttpServletRequest request) {
Long totalCount = new Long(registerService.pageCounts());
//System.out.println(totalCount);
String sqlStr = "select * from 表名 where sex='男'";
Page page = executePage(request,sqlStr,totalCount,"id desc");
List<UserAll(pojo)> users = registerService.pageList(page.getQuerySql());
return new ModelAndView("register/register_list","users",users);
}

2.service層

程式碼:

public int pageCounts() {
// TODO Auto-generated method stub
return registerDao.pageCounts();
}

public List<UserAll> pageList(String querySql) {

return registerDao.pageList(querySql);
}

3.dao層

public int pageCounts() {
String sqlStr = "select count(*) from register";
return jdbcTemplate.queryForInt(sqlStr, new Object[] {});
}
public List<UserAll> pageList(String querySql) {
// TODO Auto-generated method stub
return simpleJdbcTemplate.query(querySql, new BeanPropertyRowMapper<UserAll>(UserAll.class));
}

三.jsp頁面

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="/WEB-INF/c.tld" prefix="c"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<head>
<%@ include file="/common/meta.jsp"%>
</head>
<html>
<body>

<jsp:include page="/page/page.jsp">
<jsp:param name="url" value="registerlist.htm" />
</jsp:include>
</body>
</html>