标题:java web分页错误
只看楼主
zcdjt
Rank: 3Rank: 3
等 级:论坛游侠
威 望:4
帖 子:99
专家分:181
注 册:2014-9-9
结帖率:85.71%
已结贴  问题点数:20 回复次数:5 
java web分页错误
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]
数据库中学生数据显示不出来,记录,几页没数据,求大神指导一下。
搜索更多相关主题的帖子: request package public import java 
2017-02-20 21:19
菠萝蜜汁
Rank: 2
等 级:论坛游民
帖 子:7
专家分:11
注 册:2016-8-26
得分:7 
新手路过,表示看不懂
2017-02-21 10:37
枫xby
Rank: 5Rank: 5
等 级:职业侠客
威 望:8
帖 子:82
专家分:371
注 册:2014-10-19
得分:7 
和默认第一页为 1 有关吗
2017-02-21 12:55
mnmn4429
Rank: 4
等 级:业余侠客
帖 子:64
专家分:245
注 册:2017-2-21
得分:7 
逻辑有问题,list集合里没有数据
2017-02-21 19:36
zcdjt
Rank: 3Rank: 3
等 级:论坛游侠
威 望:4
帖 子:99
专家分:181
注 册:2014-9-9
得分:0 
回复 4楼 mnmn4429
import stuentDao.stuDao;有数据我没粘完,前面在数据库查询出来保存在数组里了。

今朝醉
2017-02-22 10:14
编程要有思想
Rank: 1
等 级:新手上路
威 望:1
帖 子:28
专家分:1
注 册:2013-10-10
得分:0 
for (int i = 0; i < list.size(); i++) {
            if (i >= (currentPageIndex - 1) * countPerPage       //你先判断下循环有没有进入到if()里,感觉你的 list是空的,没有取到数据
                    && i < (currentPageIndex) * countPerPage) {
                pageStudents.add(list.get(i));
            }
        }
2017-02-27 15:06



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-474382-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.152176 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved