web21 分页

  • eclipse中是workspace的名字,修改工程名字需注意发布路径(真正在服务器内部名字)改没有:右键项目-properties
Paste_Image.png
  • vo(value object)专门传递值区别于实体
  • str.trim()去掉字符串空格
  • 数组在创建时候必须定义大小并且定义几个放几个而集合不限制
  • 集合转数组(list.toarray)
---------注意搜索语句多个不确定书写,注意拼接空格
public List<Product> look(VO vo) throws SQLException {
        
        QueryRunner qr = new QueryRunner(MyCurrentConn.getCombpdatasource());
        String sql="select *from product where 1=1 ";
        ArrayList<Object> arr = new ArrayList<>();
        if (vo.getPname()!=null&&!vo.getPname().trim().equals("")) {            
            sql+="  and pname like ? ";
            arr.add("%"+vo.getPname().trim()+"%");
        }
        if (vo.getCid()!=null&&!vo.getCid().trim().equals("")) {
            sql+=" and cid=? ";
            arr.add(vo.getCid().trim());
        }
        if (vo.getIs_hot()!=null&&!vo.getIs_hot().trim().equals("")) {
            sql+=" and is_hot=?";
            arr.add(vo.getIs_hot().trim());
        }
    
        List<Product> products =qr.query(sql, new BeanListHandler<Product>(Product.class),arr.toArray());
        return products;
    }

------------------jQuery获得
$(function() {
        $("#is_hot option[value='${vo.is_hot}']").prop("selected", true);
        $("#cid option[value='${vo.cid}']").prop("selected", true);
    })

  • 出现找不到文件的缓存错误就project-转换一下自动手动编译
  • jquery记得导包(<script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-1.11.3.min.js"></script>)
Paste_Image.png
Paste_Image.png
  • 分页的数据封装
public class PageBean<T> {
private int currentPage;
private int currentNum;
private int totalPage;
private int totalNum;
//每页显示数据
private List<T> currentPageProduct=new ArrayList<T>();

  • select count (*)from product;返回是一个long型
  • math.ceil(总页数/总条数)此为向上取整
  • limit ?,? 起始索引,当页条数
  • 分页
------------web-获得点击页数,每页数据写死, 将返回数据list,页数,当前页所有信息封装
- Integer.parseInt(currentPageStr);转化int

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String currentPageStr = request.getParameter("page");
        if (currentPageStr == null) {
            currentPageStr = "1";
        }
        int currentPage = Integer.parseInt(currentPageStr);
        int currentCount = 12;

        ProductListSer proListSer = new ProductListSer();
        PageBean<Product> pageBean = null;
        try {
            pageBean = proListSer.getPage(currentPage, currentCount);
        } catch (SQLException e) {

            e.printStackTrace();
        }
        request.setAttribute("pageBean", pageBean);
        request.getRequestDispatcher("/product_list.jsp").forward(request, response);

    }
----------------ser层将web层和dao层数据封装返回web层对象
public class ProductListSer {

    public PageBean<Product> getPage(int currentPage, int currentCount) throws SQLException {
//      获得总条数
//      获得分页显示数据:起始索引=(当前页-1)*当前页条数
//           总页数:math.ceil(总条数/每页条数)有小数进一位      
        
        ProductDao pDao = new ProductDao();
        int totalCount=  pDao.getTotalCount();
        
        PageBean<Product> pageBean = new PageBean<>();
        pageBean.setCurrentNum(currentCount);
        pageBean.setCurrentPage(currentPage);
        pageBean.setTotalNum(totalCount);
        pageBean.setTotalPage((int)Math.ceil(1.0*totalCount/currentCount));
    int index=(currentPage-1)*currentCount;
pageBean.setCurrentPageProduct(pDao.getCurrentPagePro(index,currentCount));
        
        return pageBean;
    }
}
--------------dao层,获取总数据条数,limit返回每页数据
- qr.query(sql, new ScalarHandler());返回long型
- "select * from product limit ?,? ";参数是:起始索引,返回条数

public int getTotalCount() throws SQLException {
        
        QueryRunner qr = new QueryRunner(MyCurrentConn.getCombpdatasource());
        String sql="select count(*) from product  ";
        Long   totalCount=(Long) qr.query(sql, new ScalarHandler());
        return totalCount.intValue();
    }

    public List<Product> getCurrentPagePro(int index,int currentCount) throws SQLException {
        QueryRunner qr = new QueryRunner(MyCurrentConn.getCombpdatasource());
        String sql="select * from product limit ?,? ";
        
        return qr.query(sql, new BeanListHandler<Product>(Product.class),index,currentCount);
    }
---------------------jsp页面,分页是三部分,中间点击页面数,前进和后退,都需要判断当前页跳转将要页面page带回
        <c:forEach items="${pageBean.currentPageProduct }" var="pro">
            <div class="col-md-2" style="height: 250px">
                <a href="product_info.htm"> <img
                    src="${pageContext.request.contextPath }/${pro.pimage}" width="170"
                    height="170" style="display: inline-block;">
                </a>
                <p>
                    <a href="product_info.html" style='color: green'>${pro.pname}</a>
                </p>
                <p>
                    <font color="#FF0000">商城价:¥${pro.shop_price}</font>
                </p>
            </div>
        </c:forEach>

    </div>

    <!--分页 -->
    <div style="width: 380px; margin: 0 auto; margin-top: 50px;">
        <ul class="pagination" style="text-align: center; margin-top: 10px;">
            <!-- 判断返回 -->
            <c:if test="${pageBean.currentPage==1 }">
                <li class="disabled"><a href="javascript:void(0);"
                    aria-label="Previous"><span aria-hidden="true">«</span></a></li>
            </c:if>
            <c:if test="${pageBean.currentPage!=1 }">
                <li><a
                    href="${pageContext.request.contextPath }/productList?page=${pageBean.currentPage-1 }"
                    aria-label="Previous"><span aria-hidden="true">«</span></a></li>
            </c:if>

            <!-- 判断当前页 -->
            <c:forEach begin="1" end="${pageBean.totalPage }" var="page">
                <c:if test="${pageBean.currentPage==page }">
                    <li class="active"><a href="javaScript:void(0);">${page }</a></li>
                </c:if>
                <c:if test="${pageBean.currentPage!=page }">
                    <li><a
                        href="${pageContext.request.contextPath }/productList?page=${page }">${page }</a></li>
                </c:if>
            </c:forEach>

            <!-- 判断向后 -->
            <c:if test="${pageBean.currentPage==pageBean.totalPage }">
                <li class="disabled"><a href="javaScript:void(0);"
                    aria-label="Next"> <span aria-hidden="true">»</span>
                </a></li>
            </c:if>
            <c:if test="${pageBean.currentPage!=pageBean.totalPage }">
                <li><a
                    href="${pageContext.request.contextPath }/productList?page=${pageBean.currentPage+1 }"
                    aria-label="Next"> <span aria-hidden="true">»</span>
                </a></li>

            </c:if>
Paste_Image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 33,799评论 18 399
  • 来源与:阿里云栖 禁止用于商业用途 ps:如果需要电子书 评论你们邮箱 我会发给你们 下面感觉还是有点乱 目录 一...
    小向资源网阅读 12,293评论 0 12
  • 第十天 权限修饰符 public protected default private 同一类 true true ...
    炙冰阅读 3,619评论 0 1
  • 白天得一空闲 穿过小街 到达目的地 我们走了一圈 发现了这个拍照的好地儿 一排红砖围墙 院子里种的是葡萄树
    白瑪_ceaf阅读 1,202评论 0 0
  • 蓝色与白色交融是什么样子呢 抬头看看天的颜色 如此纯净 毫无杂质 心情也会跟着好起来 风挺冷 阳光灿烂 站在土地上...
    always过路人阅读 1,256评论 0 0

友情链接更多精彩内容