分页

心已入冬 提交于 2020-01-23 21:01:54

package cn.itcast.customer.web.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.itcast.customer.domain.Customer;
import cn.itcast.customer.domain.PageBean;
import cn.itcast.customer.service.CustomerService;

public class CustomerFindAllByPageServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        System.out.println(request.getRemoteAddr());

        // 1.默认访问第一页
        int pageNum = 1;

        String _pageNum = request.getParameter("pageNum");
        if (_pageNum != null) {
            pageNum = Integer.parseInt(_pageNum);
        }

        // 2.每页条数 人为定义
        int currentPage = 5;

        String _currentPage = request.getParameter("currentPage");
        if (_currentPage != null) {
            currentPage = Integer.parseInt(_currentPage);
        }

        // 3.调用service,完成查询当前页数据操作
        CustomerService service = new CustomerService();

        try {
            PageBean pb = service.findByPage(pageNum, currentPage);

            // 4.将数据存储到request域,请求转到到页面展示。

            request.setAttribute("pb", pb);

            request.getRequestDispatcher("/showCustomerByPage.jsp").forward(
                    request, response);
            return;
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

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

}


showCustomerByPage.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<%@taglib prefix="my" uri="http://www.itcast.cn/tag"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<title>My JSP 'index.jsp' starting page</title>

<script type="text/javascript">
    function changeCurrentPage(value) {

        location.href = "/day20_1/findAllByPage?currentPage=" + value;
    };
</script>
</head>

<body>

    <c:if test="${empty pb.cs}">
        无客户信息
    </c:if>


    <c:if test="${not empty pb.cs}">

        <table border="1" align="center" width="85%">
            <tr>

                <td>客户编号</td>
                <td>客户姓名</td>
                <td>客户性别</td>
                <td>客户生日</td>
                <td>客户电话</td>
                <td>客户邮箱</td>
                <td>客户爱好</td>
                <td>客户类型</td>
                <td>客户备注</td>

            </tr>

            <c:forEach items="${pb.cs}" var="c">
                <tr>

                    <td>${c.id }</td>
                    <td>${c.name}</td>
                    <td>${c.gender }</td>
                    <td>${c.birthday }</td>
                    <td>${c.cellphone }</td>
                    <td>${c.email }</td>
                    <td>${c.preference }</td>
                    <td>${c.type }</td>
                    <td>${c.description }</td>

                </tr>
            </c:forEach>
            <tr>
                <td colspan="9" align="center"><a
                    href="/day20_1/findAllByPage?pageNum=1&currentPage=${pb.currentPage}">首页</a>&nbsp;&nbsp;&nbsp;

                    <c:if test="${pb.pageNum==1}">
                            上一页&nbsp;&nbsp;&nbsp;
                        </c:if> <c:if test="${pb.pageNum!=1}">
                        <a
                            href="/day20_1/findAllByPage?pageNum=${pb.pageNum-1}&currentPage=${pb.currentPage}">上一页</a>&nbsp;&nbsp;&nbsp;
                        </c:if> <c:if test="${pb.pageNum==pb.totalPage}">
                            下一页&nbsp;&nbsp;&nbsp;
                        </c:if> <c:if test="${pb.pageNum!=pb.totalPage}">
                        <a
                            href="/day20_1/findAllByPage?pageNum=${pb.pageNum+1 }&currentPage=${pb.currentPage}">下一页</a>&nbsp;&nbsp;&nbsp;
                        </c:if> <a
                    href="/day20_1/findAllByPage?pageNum=${pb.totalPage }&currentPage=${pb.currentPage}">尾页</a>&nbsp;&nbsp;&nbsp;

                    <select name="currentPage"
                    οnchange="changeCurrentPage(this.value);">
                        <option>--请选择每页条数--</option>
                        <option value="5">5</option>
                        <option value="10">10</option>
                        <option value="20">20</option>
                </select>
                </td>

            </tr>
            <tr>
                <td colspan="9" align="center"><c:forEach begin="1"
                        end="${pb.totalPage}" var="n" step="1">
                        <c:if test="${n==pb.pageNum}">
                            <a
                                href="/day20_1/findAllByPage?pageNum=${n}&currentPage=${pb.currentPage}"><font
                                color='red'>第${n}页</font> </a>&nbsp;&nbsp;
                            </c:if>

                        <c:if test="${n!=pb.pageNum}">
                            <a
                                href="/day20_1/findAllByPage?pageNum=${n}&currentPage=${pb.currentPage}">第${n}页</a>&nbsp;&nbsp;
                    
                                </c:if>
                    </c:forEach>
                </td>
            </tr>
            <tr>
                <td colspan="9" align="center"><my:page pb="${pb}" />
                </td>
            </tr>
        </table>
    </c:if>

</body>
</html>

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!