在http://blog.csdn.net/xanlv/article/details/53318076 java–分頁技術(1)的基礎上修改了一部分
show.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<script type="text/javascript">
function sub(obj){
window.location.href="<c:url value='/page?pageNo="+obj.value+"'/>";
}
</script>
</head>
<body>
<h3>學生信息</h3>
<c:forEach items="${result.students}" var="s">
id=${s.id},,,name=${s.name}<br/>
</c:forEach>
pageCount=${result.pageCount}<br/>
showStart=${showStart},${showEnd}
<h2>////////////展示分頁的分頁////////////</h2>
<c:if test="${result.currentPage>1}">
<a href='<c:url value="/page?pageNo=${result.currentPage-1}"></c:url>'>上一頁</a>
</c:if>
<c:forEach begin="${showStart}" end="${showEnd}" var="idx">
<c:if test="${result.currentPage==idx}">
${idx}
</c:if>
<c:if test="${result.currentPage!=idx}">
<a href='<c:url value="/page?pageNo=${idx}"></c:url>'>${idx}</a>
</c:if>
</c:forEach>
<c:if test="${result.currentPage<result.pageCount}">
<a href='<c:url value="/page?pageNo=${result.currentPage+1}"></c:url>'>下一頁</a>
</c:if>
<br/><br/>
<select onchange="sub(this)">
<c:forEach begin="1" end="${result.pageCount}" var="idx">
<%--
<option selected='<c:if test="${idx==result.currentPage}">selected</c:if>' value="${idx}">第${idx}頁</option>
--%>
<option <c:if test="${idx==result.currentPage}">selected</c:if> value="${idx}">第${idx}頁</option>
</c:forEach>
</select>
<h2>搜素</h2>
<form action="<c:url value='/page'/>" method="post">
ID中包含:<input type="text" name="id" value="${stud.id}"/>
Name中包含:<input type="text" name="name" value="${stud.name}"/>
<input type="submit"/>
</form>
</body>
</html>
PageServlet.java
package cn.hncu.servlet;
import java.io.IOException;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.hncu.domain.Stud;
import cn.hncu.service.IPageService;
import cn.hncu.service.PageServiceImpl;
public class PageServlet extends HttpServlet {
//依賴注入service
IPageService service=new PageServiceImpl();
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Stud stud=null;
if(request.getMethod().equals("GET")){
// System.out.println("det...");
//先看看session中有沒有舊的查詢條件值對象stud
stud=(Stud) request.getSession().getAttribute("stud");
if(stud==null){
stud=new Stud();
}
}else{
//System.out.println("post....");
//用Model封裝查詢條件---更好!
String id=request.getParameter("id");
String name=request.getParameter("name");
stud=new Stud();
stud.setId(id);
stud.setName(name);
request.getSession().setAttribute("stud", stud);
}
String pageNo=request.getParameter("pageNo");
if(pageNo==null||pageNo.equals("")){
pageNo="1";//默認查詢第1頁
}
try {
//把頁碼轉成int
int iPageNo=Integer.parseInt(pageNo);
Map<String, Object> result=service.query(iPageNo,stud);
//這裏需要注意把service層和dao層對應的此方法的修改
// System.out.println("--"+result.get("pageCount"));
// System.out.println("--"+result.get("students"));
//再補一個結果3: 當前頁碼
result.put("currentPage", iPageNo);
//////////////相比上一版本增加下面這段代碼//////////////////////
//爲實現分頁的分頁,只需在servlet中爲前端補存:showStart和showEnd---顯示的起始與終止頁碼
//分頁的分頁
int showSize=10;//每次顯示幾個頁號
int showStart=1;//顯示的起始頁號
// int showEnd=showStart+showSize-1;
int showEnd=0;//顯示的終止頁號
int pageCount=Integer.valueOf(""+ result.get("pageCount"));
//如果所有頁碼不足showSize則: showStart=1, showEnd=pageCount
if(showSize > pageCount){
showEnd=pageCount;
showStart=1;
}else{
System.out.println("iPageNo:"+iPageNo);
//計算起始頁號
if(iPageNo<=(showSize/2)){
showStart=1;
}else{
showStart=iPageNo-(showSize/2);
}
//計算終止頁號
showEnd=showStart+showSize-1;
if(showEnd>pageCount){
showEnd=pageCount;
showStart=pageCount-showSize+1;
}
}
request.setAttribute("showStart", showStart);
request.setAttribute("showEnd", showEnd);
//結果數據一定要放在容器中
request.setAttribute("result", result);
//轉發到結果頁面去顯示
request.getRequestDispatcher("/jsps/show.jsp").forward(request, response);
} catch (NumberFormatException e) {
response.getWriter().println("頁碼格式錯誤!");
} catch (Exception e) {
response.getWriter().println("數據庫查詢錯誤!");
}
}
}
PageDaoJdbc.java
package cn.hncu.dao;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import cn.hncu.domain.Stud;
import cn.hncu.pubs.C3p0Utils;
public class PageDaoJdbc implements PageDAO {
@Override
public Map<String, Object> query(Integer iPageNo,Stud stud) throws Exception {
int pageSize=10;//每頁顯示20行,本例這裏寫死了
Map<String, Object> result=new HashMap<String,Object>();
//構造條件查詢的sql,包含兩條,一條用於查詢總行數,另一條查數據行
// String sql_1="select count(1) from student where 1=1";
String sql_1="select count(1) from student where 1=1";//查詢總行數
String sql_2="select * from student where 1=1";//查詢當前頁要顯示的內容(數據行)
if(stud.getId()!=null&& !stud.getId().trim().equals("")){
sql_1+=" and id like '%"+stud.getId()+"%'";
sql_2+=" and id like '%"+stud.getId()+"%'";
}
if(stud.getName()!=null&& !stud.getName().trim().equals("")){
sql_1+=" and name like '%"+stud.getName()+"%'";
sql_2+=" and name like '%"+stud.getName()+"%'";
}
System.out.println("sql_1:"+sql_1);
QueryRunner run=new QueryRunner(C3p0Utils.getDataSource());
Object obj=run.query(sql_1, new ScalarHandler());//使用ScalarHandler封裝返回結果是1行1列的數據
System.out.println("pageCount:"+obj.toString());
int rows=Integer.parseInt(obj.toString());
//計算出總頁數
// int pageCount=rows/iPageNo+rows%iPageNo==0?0:1;
int pageCount=rows/pageSize+(rows%pageSize==0?0:1);
result.put("pageCount", pageCount);//結果1
System.out.println("row:"+rows);
System.out.println("pageCount:"+pageCount);
int startN = (iPageNo-1)*pageSize;//從哪一行開始顯示
// if(startN==0){
// startN=1;
// }
sql_2+=" limit "+startN+","+pageSize;
// System.out.println("sql_2:"+sql_2);
//查詢當前頁要顯示的內容(數據行)
// List<Map<String, String>> students=run.query(sql_2, new MapListHandler());//error
List<Map<String, Object>> students=run.query(sql_2, new MapListHandler());
// System.out.println(students);
result.put("students", students);//結果2
// System.out.println("ll"+result.get("students"));
return result;
}
}
首頁
中頁
尾頁
展示查詢–首頁
展示查詢–尾頁