注册 登录
编程论坛 JAVA论坛

java web分页错误

zcdjt 发布于 2017-02-20 21:19, 3294 次点击
package stuServlet;

import
import
import java.util.ArrayList;

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

import stuentDao.stuDao;

public class stuPages extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        int countPerPage = 5;
        HttpSession session = request.getSession();
        String pageIndex = request.getParameter("pageIndex");// 默认第一页
        if (pageIndex == null) {
            pageIndex = "1";
        }
        // 首次访问,查询所有数据,计算页数。
        ArrayList list = (ArrayList) session.getAttribute("list");
        if (list == null) {
            stuDao studentDao = new stuDao();
            list = studentDao.queryAllStudents();
            session.setAttribute("list", list);
            int pageCount;
            if (list.size() % countPerPage == 0) {
                pageCount = list.size() / countPerPage;
            } else {
                pageCount = list.size() / countPerPage + 1;
            }
            session.setAttribute("pageCount", pageCount);
            session.setAttribute("studentCount", list.size());
        }
        // 根据当前页数在查询集合中获取数据
        int currentPageIndex = Integer.parseInt(pageIndex);
        ArrayList pageStudents = new ArrayList();
        for (int i = 0; i < list.size(); i++) {
            if (i >= (currentPageIndex - 1) * countPerPage
                    && i < (currentPageIndex) * countPerPage) {
                pageStudents.add(list.get(i));
            }
        }
        session.setAttribute("pageStudents", pageStudents);
        session.setAttribute("currentPageIndex", currentPageIndex);
        RequestDispatcher rd = request
                .getRequestDispatcher("/showStudents1.jsp");
        rd.forward(request, response);
    }

}

<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.
<html>
<body>
    <b>以下是学生信息</b>
    <hr>
    共${studentCount}条记录; 共${pageCount}页; 当前第 ${currentPageIndex}页;
    <table>
        <tr bgcolor="yellow">
            <td>学号</td>
            <td>姓名</td>
            <td>性别</td>
        </tr>
        <c:forEach items="${pageStudents}" var="stu">
            <tr bgcolor="pink">
                <td>${stu.stuno}</td>
                <td>${stu.stuname}</td>
                <td>${stu.stusex}</td>
            </tr>
        </c:forEach>
    </table>
    <a href="/fenfe/stuServlet/stuPages?pageIndex=1">首页</a>
    <c:if test="${currentPageIndex!=1 }">
        <a href="/fenfe/stuServlet/stuPages?pageIndex=${currentPageIndex-1 }">上一页</a>
    </c:if>
    <c:if test="${currentPageIndex!=pageCount }">
        <a href="/fenfe/stuServlet/stuPages?pageIndex=${currentPageIndex+1}">下一页</a>
    </c:if>
    <a href="/fenfe/stuServlet/stuPages?pageIndex=${pageCount}">尾页</a>
</body>
</html>
[local]1[/local]
数据库中学生数据显示不出来,记录,几页没数据,求大神指导一下。
5 回复
#2
菠萝蜜汁2017-02-21 10:37
新手路过,表示看不懂
#3
枫xby2017-02-21 12:55
和默认第一页为 1 有关吗
#4
mnmn44292017-02-21 19:36
逻辑有问题,list集合里没有数据
#5
zcdjt2017-02-22 10:14
回复 4楼 mnmn4429
import stuentDao.stuDao;有数据我没粘完,前面在数据库查询出来保存在数组里了。
#6
编程要有思想2017-02-27 15:06
for (int i = 0; i < list.size(); i++) {
            if (i >= (currentPageIndex - 1) * countPerPage       //你先判断下循环有没有进入到if()里,感觉你的 list是空的,没有取到数据
                    && i < (currentPageIndex) * countPerPage) {
                pageStudents.add(list.get(i));
            }
        }
1