java--分页技术(2)
来源:互联网 发布:投诉淘宝卖家电话多少 编辑:程序博客网 时间:2024/05/01 04:02
在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; }}
首页
中页
尾页
展示查询–首页
展示查询–尾页
0 0
- java--分页技术(2)
- java查询分页技术(2)
- java分页技术比较
- Java另一种分页技术
- java分页技术
- java分页技术
- Java中的分页技术
- java分页技术
- Java.Web - 分页技术
- Java分页技术
- java 分页技术
- Java web 分页技术
- java分页技术
- 浅谈Java分页技术
- java + struts2 分页技术
- Java分页技术
- java--分页技术(1)
- Java Web 分页技术
- 获取下拉值 及 弹出页面返回值
- 重置自增字段
- mysql建表时多个字段用到timestamp时报错怎么办
- 基础 python(二)运算符
- Python中list的交、并、差集获取方法
- java--分页技术(2)
- catch异常后不执行thy catch之后的代码
- 为什么HBase数据查询快速
- 【腾讯TMQ】移动H5性能测试平台解决方案
- Ajax基础知识
- Linux系统调用及用户编程接口(API) (三)
- 蓝桥杯入门训练第二题画圆
- Django模型应用
- MPU6050传感器数据符号位问题---被忽略的补码