Java web 分页实现的补充

                                         上一篇文章后几张图片不清晰,现补充代码如下:

 

一、 index.jsp  代码:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
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>
  </head>
  
  <body>
    This is my index page. <br>
    <form action="StudentServlet" method="Post">
      教师入口:<br>
      账号:<input type="text" name="TeacherName"/><br><br>
      密码:<input type="password" name="TeacherPassword"><br><br>&nbsp; &nbsp; &nbsp; &nbsp;
      <input type="submit" value="登录" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
      <a href="#">注册</a>
    </form>
  </body>
</html>

二、StudentServlet  代码:

package pers.ms.rfeng.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import pers.ms.rfeng.dao.TeacherDao;
import pers.ms.rfeng.dao.TeacherDaoImpl;
import pers.ms.rfeng.model.PageBean;
import pers.ms.rfeng.model.Student;
import pers.ms.rfeng.service.StudentService;

public class StudentServlet extends HttpServlet {

    private StudentService studentService = new StudentService();

    private int i = 0;
    /**
     * 获取  pc
     * @param request
     * @return
     */
    private int getPc(HttpServletRequest request) {
        int pc = 1;
        //初始化  pc 为 1
        String parm = request.getParameter("pc");
        if (parm != null && !parm.trim().isEmpty()) {
        //if 语句的判断条件不懂
            pc = Integer.parseInt(parm);
        }

        return pc;
    }

    /**
     * 获取  URL
     *
     * @param request
     * @return
     */
    private String getUrl(HttpServletRequest request) {
        String url = request.getRequestURI() + "?" + request.getQueryString();

        int index = url.lastIndexOf("&pc=");
        if (index != -1) {
            url = url.substring(0, index);
        }
        return url;
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");

        int pc = getPc(request);
        String url = getUrl(request);
        PageBean<Student> pageBean = studentService.findByPage(pc);
        pageBean.setUrl(url);
        /**
         * 保存当前查询内容
         * 跳转到内容界面 message.jsp
         */
        request.setAttribute("pb", pageBean);
        request.getRequestDispatcher("/message.jsp").forward(request, response);
    }

三、message.jsp  代码如下:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'message.jsp' starting page</title>
  </head>
  
  <body>
    This is my message page. <br>
    <table>
      <tr>
        <td>编号</td><td>名字</td><td>性别</td><td>年龄</td><td>地址</td><td>学分</td>
      </tr>
      <c:forEach items="${pb.beanlist }" var="student">
        <tr>
          <td>${student.id }</td>
          <td>${student.name }</td>
          <td>${student.gender }</td>
          <td>${student.age }</td>
          <td>${student.address }</td>
          <td>${student.grade }</td>
        </tr>
      </c:forEach>
    </table>
    <%@ include file="/tab.jsp" %>
  </body>
</html>

四、tab.jsp  代码如下:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'tab.jsp' starting page</title>
  </head>
  
  <body>
    <c:choose>
      <c:when test="${pb.pc eq null or pb.pc eq 1 }">last page</c:when>
      <c:otherwise><a href="${pb.url }&pc=${pb.pc -1 }">last page</a></c:otherwise>
    </c:choose>
    
    <c:choose>
      <c:when test="${pb.tp <6 }">
        <c:set value="1" var="begin"></c:set>
        <c:set value="${pb.tp }" var="end"></c:set>
      </c:when>
      
      <c:otherwise>
        <c:set value="${pb.pc-2 }" var="begin"></c:set>
        <c:set value="${pb.pc+3 }" var="end"></c:set>
        
        <c:if test="${pb.pc <3 }">
          <c:set value="1" var="begin"></c:set>
          <c:set value="6" var="end"></c:set>
        </c:if>
      
        
        <c:if test="${pb.pc +3 >pb.tp }">
          <c:set value="${pb.tp-5 }" var="begin"></c:set>
          <c:set value="${pb.tp }" var="end"></c:set>
        </c:if>
      </c:otherwise>
    </c:choose>
    
    <c:forEach begin="${begin }" end="${end }" var="page">
     <c:choose>
    <c:when test="${pb.pc eq page }">${page }</c:when>
    <c:otherwise><a href="${pb.url }&pc=${page}">${page }</a></c:otherwise>
    </c:choose>
    </c:forEach>
    
    <c:choose>
      <c:when test="${pb.pc eq pb.tp or pb.pc eq null }">next page</c:when>
      <c:otherwise><a href="${pb.url }&pc=${pb.pc +1}">next page</a></c:otherwise>
    </c:choose>
  </body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

niaonao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值