JavaWeb - 模糊查询 + 分页

  1. 效果图
    JavaWeb - 模糊查询 + 分页_第1张图片
  2. 核心代码(12个一页)
    
    
    <%--首页--%> <%--上一页--%> <%--下一页--%> <%--尾页--%>
    第${curPage}页/共${totalPage}页
    package com.imooc.cart.servlet;
    
    import com.imooc.cart.data.LocalCache;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    /**
     * 商品(课程)控制器
     * @version 1.0
     */
    public class ProductServlet extends HttpServlet {
    
        @Override
        protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
            String name = req.getParameter("title");
            String pageStr = req.getParameter("page");
            int page = 1;
            if (null != pageStr && !"".equals(pageStr)) {
                page = Integer.parseInt(pageStr);
            }
    
            int totalProducts = LocalCache.getProductsCount(name);
            int totalPage = totalProducts % 12 > 0 ? totalProducts / 12 + 1 : totalProducts / 12;
    
            req.setAttribute("curPage", page);
            req.setAttribute("prePage", page > 1 ? page - 1 : 1);
            req.setAttribute("nextPage", totalPage > page ? page + 1 : totalPage);
            req.setAttribute("totalPage", totalPage);
            req.setAttribute("title", name);
    
            req.setAttribute("products", LocalCache.getProducts(page, 12, name));
            req.getRequestDispatcher("/WEB-INF/views/biz/list.jsp").forward(req, resp);
        }
    }
    package com.imooc.cart.data;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    /**
     * 本地缓存
     * @version 1.0
     */
    public final class LocalCache {
    
        private static Map productMap = new HashMap<>();
        // 省略...
    
        public static List getProducts(int page, int size, String name) {
            List products = new ArrayList<>();
    
            if (null != name && !"".equals(name)) {
                productMap.values().forEach(product -> {
                    if (product.getName().contains(name)) {
                        products.add(product);
                    }
                });
            } else {
                products.addAll(productMap.values());
            }
    
            int start = (page - 1) * size;
            int end = products.size() >= page * size ? page * size : products.size();
            return products.subList(start, end);
        }
    
        public static int getProductsCount(String name) {
            List products = new ArrayList<>();
    
            if (null != name && !"".equals(name)) {
                productMap.values().forEach(product -> {
                    if (product.getName().contains(name)) {
                        products.add(product);
                    }
                });
            } else {
                products.addAll(productMap.values());
            }
            return products.size();
        }
    }

     

  3. 待更新...

你可能感兴趣的:(#,JavaWeb)