菜品名字的查询问题
我现在能够添加菜品,我的菜品展示是用了循环将所有添加的菜品进行遍历展示。
但是我在做菜品名称查询时除了问题。我查询一个菜品名称,展示出来的还是所有的菜品,并不是一个,而是所有的。
请问怎么处理,我知道问题出在这里,可是不知道怎么解决。
是在查询到的菜品名称的servlet里面,清除掉session,重新添加菜品信息吗?
还是重新新建一个菜品展示页,在那里面不写循环展示,查询哪个展示哪个?但是这样好像治标不治本。麻烦老师出来指导一下。
SelectServlet.java
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
<%@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>0
收起
正在回答 回答被采纳积分+1
4回答
从网页搭建入门Java Web2018版
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10204 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程








恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星