菜品名字的查询问题

菜品名字的查询问题

我现在能够添加菜品,我的菜品展示是用了循环将所有添加的菜品进行遍历展示。

但是我在做菜品名称查询时除了问题。我查询一个菜品名称,展示出来的还是所有的菜品,并不是一个,而是所有的。

请问怎么处理,我知道问题出在这里,可是不知道怎么解决。

是在查询到的菜品名称的servlet里面,清除掉session,重新添加菜品信息吗?

还是重新新建一个菜品展示页,在那里面不写循环展示,查询哪个展示哪个?但是这样好像治标不治本。麻烦老师出来指导一下。

SelectServlet.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package homework2.servlet;
 
import java.io.IOException;
import java.util.List;
 
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import homework2.domain.Food;
 
/**
  *  根据菜名查询的Servlet
 */
@WebServlet("/SelectServlet")
public class SelectServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 接收数据:
        String foodname = request.getParameter("foodName");
        // 从ServletContext域中获得保存菜品名称信息的集合:
        List<Food> list = (List<Food>) this.getServletContext().getAttribute("list");
        for(Food food:list) {
            // 判断菜品名称是否正确
            if(foodname.equals(food.getFoodName())) {
                // 查询成功:
                // 将菜品名称的信息保存session中
                request.getSession().setAttribute("id", food.getId());
                request.getSession().setAttribute("foodName", food.getFoodName());
                request.getSession().setAttribute("taste", food.getTaste());
                request.getSession().setAttribute("foodImage", food.getFoodImage());
                request.getSession().setAttribute("price", food.getPrice());
                request.getSession().setAttribute("description", food.getDescription());
                request.getSession().setAttribute("list", list);
                request.getSession().setAttribute("food", food);
                response.sendRedirect("/homework2/showFoodList.jsp");
                return;
            }
        }
        // 查询失败:
        request.setAttribute("msg""菜品名称错误");
        request.getRequestDispatcher("/selectFoodByName.jsp").forward(request, response);
    }
 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}

ShowFoodList.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<%@page import="homework2.domain.Food,java.util.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <%String basePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";%>
    <base href="<%=basePath%>">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>菜品信息展示</title>
    <style type="text/css">
    </style>
</head>
<body>
    <%
        List<Food> foodList = (List<Food>)session.getAttribute("foodList");
        if(foodList!=null && foodList.size()>0){
            for(Food food:foodList){
                String path = "";
                if(food.getFoodImage()!=null && !"".equals(food.getFoodImage())){
                    int index = food.getFoodImage().lastIndexOf("\\");
                    path = food.getFoodImage().substring(index);
                }
    %>
    <center>
        <h1>菜品查询</h1>
        <table border="1px" cellspacing="0px" cellpadding="0px" width="800px">
            <thead>
                <tr>
                    <th>菜品ID</th>
                    <th>菜名</th>
                    <th>口味</th>
                    <th>菜品图片</th>
                    <th>价格</th>
                    <th>菜品描述</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td><%= food.getId() %></td>
                    <td><%= food.getFoodName() %></td>
                    <td><%= food.getTaste() %></td>
                    <td><img src="upload/<%= path %>"></td>
                    <td><%= food.getPrice() %></td>
                    <td><%= food.getDescription() %></td>
                </tr>
    <%
            
        }
    %>
            </tbody>
        </table>
    </center>
</body>
</html>


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

4回答
好帮手慕柯南 2019-05-07 09:50:08

同学你好!

  1. 先来分析一下同学的代码

    http://img1.sycdn.imooc.com//climg/5cd0e2040001f22006140310.jpg

    http://img1.sycdn.imooc.com//climg/5cd0e2940001503e05860167.jpg

  2. 老师的建议是:将list里面存放所有的菜品

    http://img1.sycdn.imooc.com//climg/5cd0e2dd000170bd05540035.jpg

     foodList里面存放查询出来的菜品,如果同学想所有的菜品和单个查询的共用一个页面,同学可以传一个标识给后台,比如为1时是查询所有,不为1时根据条件查询.如果同学用两个页面直接跳两个地址就可以了。

    http://img1.sycdn.imooc.com//climg/5cd0e3120001879404220046.jpg

    如果我的回答解决了你的疑惑,请采纳,祝学习愉快~

好帮手慕柯南 2019-05-06 11:55:10

同学你好!同学代码将list保存在seesion中,首先list是所有的菜品,同学这样是将所有的菜品又给了seesion,

http://img1.sycdn.imooc.com//climg/5ccfade70001408806280329.jpg

同学在获取菜品信息时又通过foodList来获取,

http://img1.sycdn.imooc.com//climg/5ccfae920001bf0d06210192.jpg

这样同学没有正确保存查询到的菜品,也没有正确取到菜品,建议:假设list是所有的菜品,同学可以将查询到的菜品放入List<food>集合,最后放入seesion中名字为foodList,在页面中获取foodList然后循环显示菜品。如果我的回答解决了你的疑惑,请采纳,祝学习愉快~

  • 提问者 讓倪飛翔 #1
    老师,“将查询到的菜品放入List<food>集合”这个集合是我上面创建的List还是我需要重新创建一个List?
    2019-05-06 20:04:20
  • 提问者 讓倪飛翔 #2
    还有,“最后放入seesion中名字为foodList",那么我session里面岂不是只有查询的那个菜品了。在展示所有菜品的时候,没有被查询的菜品岂不是消失了?
    2019-05-06 20:06:43
提问者 讓倪飛翔 2019-05-05 19:32:18

不好意思。我可能问题分错类了。这个是最后一节项目作业的问题。因为回查课程学习直接在这里提问了。

提问者 讓倪飛翔 2019-05-05 19:24:21
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
从网页搭建入门Java Web2018版
  • 参与学习           人
  • 提交作业       1088    份
  • 解答问题       10204    个

如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!

了解课程
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师
插入代码