Paging package

Creative Commons License Copyright: Attribution, allow others to create paper-based, and must distribute paper (based on the original license agreement with the same license Creative Commons )

1, a new project to BaseDao Case (JDBC encapsulation), encapsulation paging method.
2, the introduction of the database connection jar package.
Here Insert Picture Description
3, the package code basedao introduced.
Here Insert Picture Description
4, write jsp code.
% Content% wherein such forms are prepared java code format in which jsp, c is introduced at the top jsp tag library.
Here Insert Picture Description
jsp code as follows:

<%@page import="com.aaa.util.BaseDao"%>
<%@ page language="java" import="java.util.*"  pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath =  request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
     
    <title>My JSP 'index.jsp' starting page</title>
     <meta http-equiv="pragma" content="no-cache">
     <meta http-equiv="cache-control" content="no-cache">
     <meta http-equiv="expires" content="0">   
     <meta http-equiv="keywords"  content="keyword1,keyword2,keyword3">
     <meta http-equiv="description" content="This is my page">
     <!--
     <link rel="stylesheet" type="text/css" href="styles.css">
     -->
  </head>
   
  <body>
  <% String stuPageNo=request.getParameter("pageNo");
      int pageNo=0;
      if(stuPageNo==null){
      pageNo=1;
      }else{
      pageNo=Integer.valueOf(stuPageNo);
      }
      if(pageNo<1){
      pageNo=1;
      }
      int totalCount=0;
      List<Map<String,Object>> count=BaseDao.selectMap("select  count(*) as cou from student", null);
      if(count!=null&&count.size()>0){
      totalCount=Integer.valueOf(count.get(0).get("cou")+"");
      }
      int pageSize=5;
      int  maxPage=totalCount%pageSize==0?totalCount/pageSize:totalCount/pageSize+1;
      if(pageNo>maxPage){
      pageNo=maxPage;
      }
      List<Map<String,Object>> stuList=BaseDao.selectMap("select  id,name,address,sex from student limit  "+(pageNo-1)*pageSize+","+pageSize, null);
      request.setAttribute("stuList", stuList);
      request.setAttribute("pageNo", pageNo);
      request.setAttribute("totalCount", totalCount);
      request.setAttribute("maxPage", maxPage);
   %>
   <table border="1" width="400px">
   <tr align="center">
           <th>ID</th>
           <th>姓名</th>
           <th>地址</th>
           <th>性别</th>
   </tr>
   <c:forEach items="${stuList}" var="stu">
           <tr align="center">
                <td>${stu.id }</td>
                <td>${stu.name }</td>
                <td>${stu.address }</td>
                <td>${stu.sex }</td>
           </tr>
   </c:forEach>
   <tr>
           <td colspan="4">
           <a href="index.jsp?pageNo=1">首页</a> 
           <a href="index.jsp?pageNo=${pageNo-1 }">上一页</a> 
           <a href="index.jsp?pageNo=${pageNo+1 }">下一页</a> 
           <a href="index.jsp?pageNo=${maxPage }">尾页</a> 
           跳转到<select onchange="togo(this.value)">
           <c:forEach var="page" begin="1" end="${maxPage}">
           <c:choose>
           <c:when test="${page==pageNo}">
           <option selected="selected" value="${page }">${page  }</option>
           </c:when>
           <c:otherwise>
           <option value="${page }">${page }</option>
           </c:otherwise>
           </c:choose>
           </c:forEach>
           </select>页
           共${totalCount }条${maxPage}页
           </td>
   </tr>
   </table>
   <script type="text/javascript">
   function togo(v){
   location.href="index.jsp?pageNo="+v;
   }
   </script>
  </body>
</html>

5, start tomcat, enter localhost: 8080 / project name /index.jsp visit will be as follows paging query results.
Here Insert Picture Description
These are the most basic ideas, you can put the jsp java script to write a class encapsulate, and the pageNo sql statement as a request to save the parameters passed to the domain after the class inside, get all the property values, and finally forwarded to the index. jsp page.

Guess you like

Origin blog.csdn.net/weixin_44001965/article/details/92578485