A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

Servlert代码如下:
[Java] 纯文本查看 复制代码
@WebServlet(urlPatterns = "/pageServlet")
public class pageServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        //获得当前页码
        int curPage = 1;
        String currPage = request.getParameter("curPage");
        if(currPage!=null){
            curPage = Integer.parseInt(currPage);
        }
        int pageSize = 5;//设置默认每页显示4条数据

        //调用业务
        UserService  bs = new UserService();
        PageBean pb = bs.findBooksByPage(curPage,pageSize);
        //分发转向
        request.setAttribute("pb", pb);
        request.getRequestDispatcher("/list.jsp").forward(request, response);
    }
}

Service业务层代码如下:
[Java] 纯文本查看 复制代码
public PageBean findBooksByPage(int curPage, int pageSize) {
    PageBean pb = null;
    try {
        UserDao bd = new UserDao();
        //求出总条数
        int count = bd.count();
        //求出总页数
        int totalPage = (int) Math.ceil(count*1.0/pageSize);//count%4==0 ? count/4 : count/4+1;
        //求出list集合数据(当前页的图书数据)
        List<User> list = bd.queryByPageUsers(curPage,pageSize);
        //封装数据到PageBean对象中
        pb = new PageBean();
        //设置表中的总条数
        pb.setCount(count);
        //设置当前页
        pb.setCurPage(curPage);
        //设置每页显示的条数
        pb.setPageSize(pageSize);
        //设置总页数
        pb.setTotalPage(totalPage);
        //设置查询数据
        pb.setList(list);
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return pb;

}

Dao代码如下:
[Java] 纯文本查看 复制代码
//求当前页的分页数据(用户信息)
public List<User> queryByPageUsers(int curPage, int pageSize) throws SQLException {
    JdbcTemplate jdbcTemplate = new JdbcTemplate(JdbcTemplateUtils.getDataSource());
    //参数1:从第几行开始查。参数2:每页查几条
    return jdbcTemplate.query("select * from t_user limit ?,?",
            new BeanPropertyRowMapper<>(User.class),(curPage-1)*pageSize,pageSize);
}
//求总条数
public int count() throws SQLException {
    JdbcTemplate jdbcTemplate = new JdbcTemplate(JdbcTemplateUtils.getDataSource());
    String sql = "select count(*) from t_user";
    Integer count = jdbcTemplate.queryForObject(sql, int.class);
    return count.intValue();
}

PageBean代码如下:
[Java] 纯文本查看 复制代码
public class PageBean {
   private int count; // 表中的总条数
   private int totalPage; // 求总页数
   private int curPage; //当前页
   private int pageSize; //每页显示的条数
   private List<User> list; //分页页面显示集合数据
   public int getCount() {
      return count;
   }
   public void setCount(int count) {
      this.count = count;
   }
   public int getTotalPage() {
      return totalPage;
   }
   public void setTotalPage(int totalPage) {
      this.totalPage = totalPage;
   }
   public int getCurPage() {
      return curPage;
   }
   public void setCurPage(int curPage) {
      this.curPage = curPage;
   }
   public int getPageSize() {
      return pageSize;
   }
   public void setPageSize(int pageSize) {
      this.pageSize = pageSize;
   }
   public List<User> getList() {
      return list;
   }
   public void setList(List<User> list) {
      this.list = list;
   }
}

Jsp代码如下:
[HTML] 纯文本查看 复制代码
<nav>
    <ul class="pagination">
        <%-- 上一页 --%>
        <c:if test="${pb.curPage==1}">
            <li class="disabled">
                <a href="#" >
                    <span>«</span>
                </a>
            </li>
        </c:if>

        <c:if test="${pb.curPage!=1}">
            <li>
                <a href="/pageServlet?curPage=${pb.curPage-1}" >
                    <span>«</span>
                </a>
            </li>
        </c:if>

        <%-- 页码显示区 --%>
        <c:forEach begin="1" end="${pb.totalPage}" var="n">
            <c:if test="${pb.curPage==n}">
                <li class="active"><span style="color: red">${n}</span></li>
            </c:if>
            <c:if test="${pb.curPage!=n}">
                <li><a href="/pageServlet?curPage=${n}">${n}</a></li>
            </c:if>
        </c:forEach>

        <%-- 下一页 --%>
        <c:if test="${pb.curPage==pb.totalPage}">
            <li class="disabled">
                <a href="#" >
                    <span>»</span>
                </a>
            </li>
        </c:if>

        <c:if test="${pb.curPage!=pb.totalPage}">
            <li>
                <a href="/pageServlet?curPage=${pb.curPage+1}" >
                    <span>»</span>
                </a>
            </li>
        </c:if>

    </ul>
</nav>

1 个回复

倒序浏览
奈斯
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马