javaweb实现简单分页基本步骤分析

我们以图书部分网站为例

第一步:PageServlet的设计

          ①String category = request.getParameter("category");//获取图书类别

           ② if(category==null){
category="";
}//进行判空操作

           ③//初始化每页显示的记录数

              int pageSize = 4;

            ④获取当前页

               int currentPage = 1;//当前页-------第一次加载时用到
String currPage = request.getParameter("currentPage");//从上一页或下一页得到的数据
if(currPage!=null&&!"".equals(currPage)){//第一次访问资源时,currPage可能是null
currentPage = Integer.parseInt(currPage);
}

           ⑤转到service层进行数据处理,把这些参数都传进去,这里的PageBean很重要,你需要的数据都在里面,最后要把这个对 象传到jsp页面上去。



               ProductService bs = new ProductService();
//分页查询,并返回PageBean对象
PageBean pb = bs.findBooksPage(currentPage,pageSize,category);

           ⑥在ProductService所要做的操作有:如下                    主要是得到下一页的集合数据PageBean

             public PageBean findBooksPage(int currentPage, int pageSize,String category) {

try {
int count  = productDao.count(category);//得到总记录数
int totalPage = (int)Math.ceil(count*1.0/pageSize); //求出总页数

//将当前页的后四个书籍的基本信息封装到list集合中
List<Product> products= productDao.findBooks(currentPage,pageSize,category);

//把5个变量封装到PageBean中,做为返回值
PageBean pb = new PageBean();
pb.setProducts(products);
pb.setCount(count);
pb.setCurrentPage(currentPage);
pb.setPageSize(pageSize);
pb.setTotalPage(totalPage);
//在pageBean中添加属性,用于点击上一页或下一页时使用
pb.setCategory(category);

return pb;
     } catch (SQLException e) {
e.printStackTrace();
     }
   return null;
         }

              ⑦    从service中调用dao层,已完成对数据库的操作

                       int count  = productDao.count(category);//得到总记录数

                       List<Product> products= productDao.findBooks(currentPage,pageSize,category);

                       这两句代码用到,第一句简单,主要是第二句

                       对数据库的操作如下:

                        public List<Product> findBooks(int currentPage, int pageSize,String category) throws SQLException {
              QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());
              String sql = "select * from products where 1=1";
              List list = new ArrayList();
              if(!"".equals(category)){
      sql+=" and category=?";
      list.add(category);
              }
             sql+=" limit ?,?";
             //limit用法如下:
             //SELECT * FROM table LIMIT 5,10; //检索记录行6-15
             // select * from products where 1=1 and category=? limit ?,?;
             list.add((currentPage-1)*pageSize);
             list.add(pageSize);
             //把三个参数封装在list数组中,第一个:category  第二个:(currentPage-1)*pageSize 第三个:pageSize
             return qr.query(sql, new BeanListHandler<Product>(Product.class),list.toArray());
                }

             ⑧ 到此,后台的操作就完了,直接从servlet返回到jsp面

                request.setAttribute("pb", pb);
request.getRequestDispatcher("/product_list.jsp").forward(request, response);

            ⑨设计jsp页面,这里用的是table,foreach语句,这里的pb正是PageBean对象

               <table cellspacing="0" class="booklist">
           <tr>
<c:forEach items="${pb.products }" var="b">
<td>
<div class="divbookpic">
<p>
      <a href="#"><img src="${b.imgurl}" width="115" height="129" border="0" /> </a>
</p>
</div>
<div class="divlisttitle">
              <a href="${pageContext.request.contextPath }/findBookInfoServlet?id=${b.id}">书                                                                   名:${b.name}<br />售价:${b.price } </a>
</div>
</td>
</c:forEach>
   </tr>
</table>

              ⑩最后就是下一页和上一页的按钮的响应了,html代码如下,

                      <div class="pagination">
  <ul>
                                   <li class="disablepage">

                                         <a href="${pageContext.request.contextPath  }/pageServlet?                                                                                             currentPage=${pb.currentPage==1?1:pb.currentPage-1}&category=${pb.category}">

                                                 &lt;&lt;上一页</a>
    </li>
    <li>第${pb.currentPage }页/共${pb.totalPage }页</li>
    <li class="nextPage">

                                          <a href="${pageContext.request.contextPath  }/pageServlet?                                                                                                  currentPage=${pb.currentPage==pb.totalPage?pb.totalPage:pb.currentPage+1}

                                                     &category=${pb.category}">&lt;&lt;下一页</a>
    </li>
   </ul>
</div>

                   点击的时候再次调用PageServlet,并且传递的必要的参数。

           文章到此结束,希望赞同的人儿点个赞支持一下!!!!!谢谢

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值