jsp 实现分页操作

  1. 分页依据:
    select 字段列表 from 表名 limit m,n;
    m: 表示起始记录,并且从0开始
    n: 查询记录的个数,每页记录数
  2. 分页信息
    共多少页
    有没有上一页
    有没有下一页
    当前页
    注:分页信息类Page
    注2:创建分页信息辅助类PageUtil
    public static Page createPage(int everyPage,int totalCount,int currentPage)
    everyPage: 程序员定
    totalCount: 总记录数,查询数据库表记录 select count(*) from 表名
    currentPage: 从默认第一页开始,下一页= 当前页+1 上一页 = 当前页-1
  3. 分页数据集合List
    依据查询语句获得集合: select 字段列表 from 表名 limit m,n;
    m: beginIndex
    n: everyPage

具体实现:::

UserBiz:
//分页

public int getCont();
public List<User> findByPage(Page page);

UserBizImpl:

@Override
    public int getCont() {
        String sql = "select count(*) as count from user";
        CountUtil count = (CountUtil) udao.get(sql, CountUtil.class);
        return count.getCount();
    }

    @Override
    public List<User> findByPage(Page page) {
        String sql = "select * from user limit "+page.getBeginIndex()+", "+page.getEveryPage();
        return udao.query(sql, User.class);
    }

servlet::UserServlet

int everyPage = 5;//每页记录数
        int totalCount = ubiz.getCont();//获取总记录数
        //点击链接重新获取当前页
String scurrentPage = request.getParameter("currentPage");
        int currentPage = 1; //当前页,默认1
        if(scurrentPage == null){
            currentPage = 1;//从第一页开始访问
        }else{
            currentPage = Integer.parseInt(scurrentPage);
        }
        //分页信息
        Page page = PageUtil.createPage(everyPage, totalCount, currentPage);
        //分页数据信息
        List<User> list = ubiz.findByPage(page);

        request.setAttribute("page", page);
        request.setAttribute("list", list);
        //转发到userlist.jsp
        request.getRequestDispatcher("/back/manager/userlist.jsp").forward(request, response);

userlist.jsp中的分页表现

<table width="461" height="24" border="1" cellpadding="0" cellspacing="0">
  <tr>
    <td width="199">当前为第${page.currentPage}页,共${page.totalPage}页</td>
    <td width="256">
    <c:choose>
        <c:when test="${page.hasPrePage}">
            <a href="<%=path %>/user.do?method=list&currentPage=1">首页</a> | 
    <a href="<%=path %>/user.do?method=list&currentPage=${page.currentPage -1 }">上一页</a>
        </c:when>
        <c:otherwise>
            首页 | 上一页
        </c:otherwise>
    </c:choose>

    <c:choose>
        <c:when test="${page.hasNextPage}">
            <a href="<%=path %>/user.do?method=list&currentPage=${page.currentPage + 1 }">下一页</a> | 
    <a href="<%=path %>/user.do?method=list&currentPage=${page.totalPage }">尾页</a>
        </c:when>
        <c:otherwise>
            下一页 | 尾页
        </c:otherwise>
    </c:choose>

</td>
  </tr>
</table>

附::::page类(分页信息实体类)

public class Page {
    private int everyPage;          //每页显示记录数
    private int totalCount;         //总记录数
    private int totalPage;          //总页数
    private int currentPage;        //当前页
    private int beginIndex;         //查询起始点
    private boolean hasPrePage;     //是否有上一页
    private boolean hasNextPage;    //是否有下一页
    public Page(int everyPage, int totalCount, int totalPage, 
            int currentPage,int beginIndex, boolean hasPrePage,
            boolean hasNextPage) {  //自定义构造方法
        this.everyPage = everyPage;
        this.totalCount = totalCount;
        this.totalPage = totalPage;
        this.currentPage = currentPage;
        this.beginIndex = beginIndex;
        this.hasPrePage = hasPrePage;
        this.hasNextPage = hasNextPage;
    }
    ///get,set方法略
}

附:pageutil 分页信息辅助类

public class PageUtil {
    //创建Page对象
    public static Page createPage(int everyPage,int totalCount,int currentPage) {//创建分页信息对象
        everyPage = getEveryPage(everyPage);
        currentPage = getCurrentPage(currentPage);
        int totalPage = getTotalPage(everyPage, totalCount);
        int beginIndex = getBeginIndex(everyPage, currentPage);
        boolean hasPrePage = getHasPrePage(currentPage);
        boolean hasNextPage = getHasNextPage(totalPage, currentPage);
        return new Page(everyPage, totalCount, totalPage, currentPage,
                beginIndex, hasPrePage,  hasNextPage);
    }
    // 以下方法辅助创建Page对象
    public static int getEveryPage(int everyPage) {     //获得每页显示记录数
        return everyPage == 0 ? 10 : everyPage;
    }
    public static int getCurrentPage(int currentPage) { //获得当前页
        return currentPage == 0 ? 1 : currentPage;
    }
    public static int getTotalPage(int everyPage,int totalCount) {//获得总页数
        int totalPage = 0;
        if(totalCount != 0 &&totalCount % everyPage == 0) {
            totalPage = totalCount / everyPage;
        } else {
            totalPage = totalCount / everyPage + 1;
        }
        return totalPage;
    }
    public static int getBeginIndex(int everyPage,int currentPage) {//获得起始位置
        return (currentPage - 1) * everyPage;
    }
    public static boolean getHasPrePage(int currentPage) {//获得是否有上一页
        return currentPage == 1 ? false : true;
    }
    public static boolean getHasNextPage(int totalPage, int currentPage) {  //获得是否有上一页
        return currentPage == totalPage || totalPage == 0 ? false : true;
    }
}
  • 24
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值