【SSM项目demo】JAVA实现表格分页功能

PageInfo.java 存储页数信息

public class PageInfo<T> {
    // 当前页需要展示的数据
    private List<T> list;
    private int totalPage;      // 总页数
    private int totalCount;     // 总记录数
    private int size;           // 每页数据量
    private int currentPage;    // 当前页数

    public PageInfo() {
    }

    public PageInfo(List<T> list, int totalPage, int totalCount, int size, int currentPage) {
        this.list = list;
        this.totalPage = totalPage;
        this.totalCount = totalCount;
        this.size = size;
        this.currentPage = currentPage;
    }
}

Handler.java

@RequestMapping("/findAll03.do")
public ModelAndView findAllByPage(@RequestParam(defaultValue = "1") int currentPage,@RequestParam(defaultValue = "0") int type,String username,HttpSession session) {
        if (type==1){
            session.setAttribute("username",username);
        }
        else {
            session.setAttribute("username", null);
        }
    ModelAndView mv = new ModelAndView();
    PageInfo<User> pageInfo = userService.findByPage(currentPage,username);
    System.out.println(pageInfo);
    mv.setViewName("page/dataList");
    mv.addObject("pageInfo", pageInfo);
    return mv;
    }

userServiceImpl.java

@Override
    public PageInfo<User> findByPage(int currentPage,String username){
        PageInfo<User> pageInfo = new PageInfo<>();
        // 指定每页的数据量
        pageInfo.setSize(5);

        // 指定总数据量
        int totalCount = userDao.getTotal(username);
        pageInfo.setTotalCount(totalCount);

        // 指定总页数   ceil() 向上取整  floor() 向下取整  round() 四舍五入
        int totalPage = (int) Math.ceil(totalCount / (double)pageInfo.getSize());
        pageInfo.setTotalPage(totalPage);

        // 判断当前页是否合理
        if (currentPage < 1) {
            pageInfo.setCurrentPage(1);
        } else if (currentPage > totalPage) {
            pageInfo.setCurrentPage(totalPage);
        } else {
            pageInfo.setCurrentPage(currentPage);
        }

        // 指定当前页的数据   指定sql语句中的两个参数
        int start = (pageInfo.getCurrentPage() - 1) * pageInfo.getSize();
        List<User> list = userDao.findAllByPage(start, pageInfo.getSize(),username);
        pageInfo.setList(list);

        return pageInfo;
    }

dataList.jsp

<table id="dataList" class="table table-bordered table-striped table-hover dataTable">         
    <thead>
    <tr>
        <th class="" style="padding-right:0px;">
            <input id="selall" type="checkbox" class="icheckbox_square-blue">
        </th>
        <th class="sorting_asc">ID</th>
        <th class="sorting_desc">用户名</th>
        <th class="sorting_asc sorting_asc_disabled">密码</th>
        <th class="sorting_desc sorting_desc_disabled">年龄</th>
    </tr>
    </thead>
    <tbody>
    <%-- c:forEach  JSTL 提供的遍历标签  --%>
    <c:forEach items="${pageInfo.list}" var="user">
        <tr>
            <td><input name="ids" type="checkbox" value="${user.id}"></td>
            <td>${user.id}</td>
            <td>${user.username}</td>
            <td>${user.password}</td>
            <td>${user.age}</td>
       </tr>
    </c:forEach>
    </tbody>
</table>
<div class="box-footer">
    <div class="pull-left">
        <div class="form-group form-inline">总共${pageInfo.totalPage} 页,共${pageInfo.totalCount} 条数据。 每页
            <select class="form-control">
                <option>1</option>
                <option>2</option>
                <option>3</option>
                <option>4</option>
                <option>5</option>
           </select> 条
        </div>
    </div>
    <div class="box-tools pull-right">
        <ul class="pagination">
            <li><a href="${pageContext.request.contextPath}/user/findAll03.do?currentPage=1" aria-label="Previous">首页</a></li>
            <li><a href="${pageContext.request.contextPath}/user/findAll03.do?currentPage=${pageInfo.currentPage - 1}">上一页</a></li>
            <c:forEach begin="1" end="${pageInfo.totalPage}" var="pageNum">
                <li><a href="${pageContext.request.contextPath}/user/findAll03.do?currentPage=${pageNum}">${pageNum}</a></li>
            </c:forEach>
            <li><a href="${pageContext.request.contextPath}/user/findAll03.do?currentPage=${pageInfo.currentPage + 1}">下一页</a></li>
            <li><a href="${pageContext.request.contextPath}/user/findAll03.do?currentPage=${pageInfo.totalPage}" aria-label="Next">尾页</a></li>
        </ul>
    </div>
</div>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值