JavaWeb - 模糊查询 + 分页

本文详细介绍了如何在Web应用中实现商品搜索功能和分页显示,通过具体代码示例展示了搜索表单的设计、分页链接的生成以及后端控制器的逻辑处理。文章重点讲解了如何根据用户输入进行数据库查询,并将结果按页展示,同时保持搜索条件的一致性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 效果图
  2. 核心代码(12个一页)
    <!-- 搜索 -->
    <form id="searchForm" method="post" action="/product/list.do">
    	<div class="search">
    		<input type="hidden" name="page" value="1">
    		<input type="text" name="title" class="search_input" value="${title}">
    		<input type="submit" class="search_submit" value="">
    	</div>
    </form>
    
    <!-- 分页 -->
    <div>
    	<%--<a href="/product/list.do?page=1">首页</a>--%>
    	<%--<a href="/product/list.do?page=${prePage}">上一页</a>--%>
    	<%--<a href="/product/list.do?page=${nextPage}">下一页</a>--%>
    	<%--<a href="/product/list.do?page=${totalPage}">尾页</a>--%>
    
    	<form method="post" action="/product/list.do" style="display: inline">
    		<input type="hidden" name="page" value="1">
    		<input type="hidden" name="title" value="${title}">
    		<input type="submit" value="首页" class="btn">
    	</form>
    	<form method="post" action="/product/list.do" style="display: inline">
    		<input type="hidden" name="page" value="${prePage}">
    		<input type="hidden" name="title" value="${title}">
    		<input type="submit" value="上一页" class="btn">
    	</form>
    	<form method="post" action="/product/list.do" style="display: inline">
    		<input type="hidden" name="page" value="${nextPage}">
    		<input type="hidden" name="title" value="${title}">
    		<input type="submit" value="下一页" class="btn">
    	</form>
    	<form method="post" action="/product/list.do" style="display: inline">
    		<input type="hidden" name="page" value="${totalPage}">
    		<input type="hidden" name="title" value="${title}">
    		<input type="submit" value="尾页" class="btn">
    	</form>
    
    	第${curPage}页/共${totalPage}页
    </div>
    
    <!-- 响应内容省略 -->
    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<Long, Product> productMap = new HashMap<>();
        // 省略...
    
        public static List<Product> getProducts(int page, int size, String name) {
            List<Product> 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<Product> 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. 待更新...

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陆克和他的代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值