第三天总结 之 商品管理界面的实现 之 模糊查询 与 数据在页面展示

商品管理界面的实现

模糊查询

第一步: 明确 查询时 需要的 条件 即sql语句中 where 后的条件
如 :在这里插入图片描述
根据前端 可以发现 模糊查询可以通过 商品名称 日期 商品类型 来查询
所以在对应的controller层下的GoodsFuzzySelectServlet中首先要获取这三个属性的值 因为查询完成后需要通过分页操作显示一页的数值 所以需要获取当前的页面 以及设置每页展示的数据个数
然后在service层 通过判断 然后完善sql语句
最后在dao层 对完善后的sql语句进行操作 返回查询的存放对象的集合
将该集合存放到request域中 然后重定向到前端页面 在前端从域中读取数据 显示在页面
其中 GoodsFuzzySelectServlet的代码如下:

package com.qfedu.controller;

import com.qfedu.entity.Goods;
import com.qfedu.service.GoodsService;
import com.qfedu.service.impl.GoodsServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "GoodsFuzzySelectServlet", value = "/GoodsFuzzySelectServlet")
public class GoodsFuzzySelectServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //防止乱码
        request.setCharacterEncoding("utf-8");
        String typeId = request.getParameter("typeId");
        String goodsname = request.getParameter("goodsname");
        String deployDate = request.getParameter("deployDate");
//        System.out.println(typeId);
//        System.out.println(goodsname);
//        System.out.println(deployDate);
//        System.out.println("!!!!!!!!!!!");

        String pageNo = request.getParameter("pageNo");

        if( (deployDate==null||"".equals(deployDate)) && (typeId==null||"".equals(typeId))  &&  (goodsname ==null||"".equals(goodsname))){
            goodsname = request.getParameter("a");
            deployDate = request.getParameter("b");
            typeId = request.getParameter("c");
//            System.out.println("//");
//            System.out.println(typeId);
//            System.out.println(goodsname);
//            System.out.println(deployDate);
//            System.out.println(pageNo);
        }

        if(pageNo==null){
            pageNo="1";
        }
        Integer pNo=Integer.valueOf(pageNo);
        int pageSize=4;
        //获取一页的数据  存放到对象集合中
        GoodsService goodsService=new GoodsServiceImpl();
        List<Goods> goodses = goodsService.getOnePageGoods(goodsname, deployDate, typeId, pNo, pageSize);
        request.setAttribute("goodses",goodses);

        //获取总页数
        int pageCount=0;
        pageCount=goodsService.getpageCount(goodsname,deployDate,typeId,pageSize);


        //用 abcef分别代指 查询时候 的条件中的 name date id pageNo pageCount
        request.setAttribute("a",goodsname);
        request.setAttribute("b",deployDate);
        request.setAttribute("c",typeId);

        //将当前页和总页数也存放到request域中   更新域中的原有的页数
        request.setAttribute("pageNo",pNo);
        request.setAttribute("pageCount",pageCount);
//        System.out.println("pNo="+pNo);
//        System.out.println("pageCount="+pageCount);
        request.getRequestDispatcher("/after/goods_list.jsp?pageNo="+pNo).forward(request,response);
    }

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

GoodsServiceImpl中的getOnePageGoods方法的代码如下

 @Override
    public List<Goods> getOnePageGoods(String goodsName, String deployDate, String typeId, int pNo, int pageSize) {
        Object[] objs={(pNo-1)*pageSize,pageSize};
        String condition=null;
        if(deployDate!=null&&!"".equals(deployDate)&&typeId!=null&&!"".equals(typeId)&&goodsName !=null&&!"".equals(goodsName)) {
            //SELECT * FROM t_goods WHERE 1=1 AND deployDate ="2019-04-29" AND typeId="4" AND goodsName LIKE "%9%" LIMIT 0,4
            // AND deployDate ="2019-04-29" AND typeId="4" AND goodsName LIKE "%9%"
            //condition =" AND deployDate ='?' AND typeId='?' AND goodsName LIKE '%?%'  "
            condition = " and deployDate =  '" + deployDate + "' and typeId = '" + typeId + "' and goodsName like '%" + goodsName + "%' ";
        }else if (goodsName !=null&&!"".equals(goodsName)&&deployDate!=null&&!"".equals(deployDate)) {
            condition =" and deployDate =  '" + deployDate + "' and goodsName like '%" + goodsName + "%' ";
        }else if (deployDate!=null&&!"".equals(deployDate)&&typeId!=null&&!"".equals(typeId)) {
            condition =" and deployDate =  '" + deployDate +  "' and typeId = '" + typeId + "' ";
        }else if (goodsName !=null&&!"".equals(goodsName)&&typeId!=null&&!"".equals(typeId)) {
            condition = " and typeId = '" + typeId + "' and goodsName like '%" + goodsName + "%' ";
        }else if(goodsName !=null&&!"".equals(goodsName)){
            condition =" and goodsName like '%" + goodsName + "%' ";
        }else if(deployDate!=null&&!"".equals(deployDate)){
            condition = " and deployDate =  '" + deployDate +"' ";
        }else if(typeId!=null&&!"".equals(typeId)){
            condition = " and typeId = '" + typeId + "'  ";
        }else {
            condition = "";
        }
//        System.out.println("-------------");
//        System.out.println(condition);
        List<Goods> onePageUsers = goodsDao.getOnePageGoods(condition, objs);
//        System.out.println(onePageUsers);
        return onePageUsers;
    }

dao层对应的getOnePageGoods方法代码如下

  @Override
    public List<Goods> getOnePageUsers(String condition, Object[] objs) {
        String sql="select * from t_goods where 1=1 "+condition+" limit ?,? ";
        return super.query(sql,objs, Goods.class);
    }

数据在页面展示

在GoodsFuzzySelectServlet中 将从数据库中查询的数据返回值为 存放对象的list集合 然后将其存放在了request域中 然后重定向到了前端页面 那么接下来就是在前端页面goods_list.jsp中展示这些数据

<%--               这里存在bug    --%>
            <c:forEach items="${goodses}" var="goods" varStatus="obj">
                <tr>
                    <td><input type="checkbox" name="goodsid"
                               value="${goods.id}" /></td>
                    <td>${goods.id}</td>
                    <td>${goods.goodsName}</td>
                    <td>${goods.price}</td>
                        <%-- ${goods.imgPath} --%>
                    <td>
                        <%
                            StringBuffer requestURL = request.getRequestURL();
                            //如果这个路径里面包含了 /after  则返回第一次遇见/after的 首位字符下标  即路径中 /after的 / 的下标
                            int i = requestURL.indexOf("/after");
                            //截取字符串  从0开始  到/after 的/ 的下标  前闭后开     截取之后即为目标路径 http://localhost:8080/javaWebday3
                            String substring = requestURL.substring(0, i);
                        %>
<%--                               图片路径存在问题             --%>
                        <img
                                src="<%= substring%>/images/goods/${goods.imgPath}"
                                class="img-rounded" height="50px" width="50px" /></td>
                    <td>${goods.date}</td>
                    <td>${goods.typeName}</td>
<%--                    bug为  这部分的问题          --%>
<%--                   参考https:// blog.csdn.net/weixin_55685975/article/details/116151780  有关button标签的属性问题           --%>
<%--         -------------------------------------------------------            --%>
                    <td>
<%--                        <button type="button" class="btn btn-primary"--%>
<%--                                data-toggle="popover" data-placement="top" title="商品介绍"--%>
<%--                                data-content="${goods.description}!">弹详情</button></td>--%>
<%--           ----------------------------------------------------------                    --%>
<%--                                   修改后               --%>
                    <button type="button" class="btn btn-primary" data-toggle="popover" data-placement="top" title="商品介绍"  data-content="${goods.comment}!">弹详情</button>
                    </td>

                    <td><a href="<%=path%>/after/add_goods.jsp?id=${goods.id}"
                           οnclick="if(confirm('是否确定编辑'+'${goods.goodsName}'+'?')==false)return false">
                        <span class="glyphicon glyphicon-edit" color="#5BC0DE"></span></a>
                    </td>

                    <td><a class="glyphicon glyphicon-trash" color="#5BC0DE"
                           style="text-decoration: none" name="goodsid"
                           href="<%=path%>/DeleteGoodsServlet?goodsid=${goods.id}"
                           οnclick="if(confirm('是否确定删除'+'${goods.goodsName}'+'?')==false)return false"></a>
                    </td>
                </tr>
            </c:forEach>
            </tbody>

其中对于图片路径存在问题 和butten 按钮属性存在问题 已解决
因为数据库中 图片的存储是一个 图片名 真实的地址为tomcat服务器下的web下的images下的goods文件夹下
在这里插入图片描述
所以在前端对其 路径进去了添加
从而通过真实的地址展示图片

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在JavaWeb中,可以使用预处理语句(PreparedStatement)来操作数据库中的数据,以避免SQL注入攻击。预处理语句可以通过设置参数的方式来动态地生成SQL语句,从而更加安全和灵活。下面是一个使用预处理语句操作数据的例子: ```java String sql = "SELECT * FROM students WHERE name LIKE ? ORDER BY id LIMIT ?, ?"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1, "%" + name + "%"); // 设置查询条件 pstmt.setInt(2, (page - 1) * pageSize); // 设置分页参数 pstmt.setInt(3, pageSize); ResultSet rs = pstmt.executeQuery(); ``` 在这个例子中,我们使用了一个带有占位符的SQL语句,其中第一个占位符表示查询条件的模糊匹配,第二个和第三个占位符表示分页参数。我们通过调用PreparedStatement类的setXXX()方法来设置占位符的值,其中XXX表示数据类型,例如setString()表示字符串类型,setInt()表示整数类型,等等。 另外,对于数据的分页展示,我们可以使用MySQL数据库的LIMIT关键字来实现。LIMIT关键字可以限制查询结果的返回数量,从而实现分页功能。例如,我们可以通过设置LIMIT 0,10来返回第一页的前10条数据,设置LIMIT 10,10来返回第二页的前10条数据,依此类推。 综上所述,我们可以通过组合使用预处理语句和LIMIT关键字来实现信息管理系统中的数据操作和分页展示功能。 ### 回答2: 在JavaWeb中,我们可以使用预处理语句来操作数据并对数据进行分页展示。预处理语句可以有效地防止SQL注入攻击,并提高系统的性能和安全性。 首先,我们需要在信息管理系统中创建一个数据库,并在数据库中创建相应的表来存储我们的数据。 接下来,我们可以在Java代码中使用预处理语句来操作数据。预处理语句允许我们在执行SQL语句之前,将参数添加到SQL语句中。这样可以避免直接将用户输入的数据拼接到SQL语句中,从而防止SQL注入攻击。 对于分页展示数据,我们可以结合使用LIMIT和OFFSET关键字来实现。LIMIT关键字可以限制查询结果的数量,而OFFSET关键字可以指定从第几条数据开始查询。我们可以根据每页显示的数量和当前页数来计算出应用的LIMIT和OFFSET值,从而实现分页展示。 具体步骤如下: 1. 获取用户输入的当前页数和每页显示的数量。 2. 计算出LIMIT和OFFSET的值,例如:LIMIT是每页显示的数量,OFFSET是(当前页数-1)乘以每页显示的数量。 3. 使用预处理语句准备SQL语句,将参数添加到SQL语句中。例如:SELECT * FROM 表名 LIMIT ? OFFSET ?。 4. 执行SQL语句,获取查询结果集。 5. 将查询结果集进行展示。 通过以上步骤,我们就可以在JavaWeb的信息管理系统中使用预处理语句操作数据,并对数据进行分页展示了。这样可以提高系统的性能和安全性,同时也可以方便用户查看数据。 ### 回答3: 在JavaWeb中的信息管理系统中,可以使用预处理语句操作数据并对数据进行分页展示。预处理语句是一种能够提高性能和安全性的数据库执行方式,可以防止SQL注入攻击。 首先,我们需要在数据库中创建一个表来存储需要管理的信息数据,例如用户表、商品表等。然后,通过JDBC连接数据库,使用预处理语句来执行查询操作。 对于数据的分页展示,我们可以使用LIMIT关键字,结合预处理语句来实现。首先,根据用户请求的页面和每页显示的数据量,计算出需要查询的数据范围。然后,编写SQL语句并替换预处理语句中的参数,设置查询的起始位置和数据量。 示例代码如下: ```java String sql = "SELECT * FROM 用户表 LIMIT ?, ?"; int currentPage = 1; // 当前页码 int pageSize = 10; // 每页显示的数据量 // 计算需要查询的数据范围 int start = (currentPage - 1) * pageSize; int length = pageSize; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setInt(1, start); pstmt.setInt(2, length); ResultSet rs = pstmt.executeQuery(); // 遍历查询结果并处理 while (rs.next()) { // 处理每条数据 } // 关闭数据库连接等资源 rs.close(); pstmt.close(); conn.close(); ``` 以上代码中,首先定义了需要查询的SQL语句,通过预处理语句将查询起始位置和数据量作为参数设置进去。然后,执行查询操作,通过ResultSet遍历结果集,处理每条数据。最后,关闭数据库连接等资源。 通过使用预处理语句操作数据,并结合LIMIT关键字来实现数据的分页展示,可以提高系统的响应速度和安全性,有效地管理和展示信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值