查询问题!
package com.imooc.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 com.imooc.food.Food; import com.imooc.food.FoodDaolmpl; /** * 菜品名称查询的Servlet */ @WebServlet("/SelectServlet") public class SelectServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public SelectServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 获取菜品的名字 String foodName = request.getParameter("foodName"); // 获取存储菜品的集合 FoodDaolmpl fdl = new FoodDaolmpl(); List<Food> foodList = fdl.getAllFood(); for(Food f : foodList) { System.out.println(f); } System.out.println(fdl.getAllFood()); // 使用菜品名称进行查询 if(fdl.getFoodByName(foodName) != null) { // 如果菜品存在,跳入到展示菜品页面 response.sendRedirect(request.getContextPath()+"/showFoodList.jsp"); }else { // 如果菜品不存在,则跳入到添加菜品页面 request.setAttribute("msg", "菜品不存在,请先添加菜品"); request.getRequestDispatcher("/addFood.jsp").forward(request, response); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package com.imooc.food; import java.util.ArrayList; import java.util.List; public class FoodDaolmpl { // 存放菜品信息的List private static final List<Food> db = new ArrayList<Food>(); // 添加菜品 public void addFood(Food food) { db.add(food); } // 查询所有菜品信息 public List<Food> getAllFood() { if (db.isEmpty()) { return null; } else { return db; } } // 根据菜品名称查询菜品信息 public Food getFoodByName(String foodName) { if(!db.isEmpty()) { for(Food f : db) { if(f.getFoodName().equals(foodName)){ return f; } } return null; }else { return null; } } // 根据菜品id查询菜品信息 public Food getFoodById(String id) { // 遍历集合判断菜品名称是否相同 for (Food f : db) { // 菜品名称相同返回该食物对象 if (f.getFoodId().equals(id)) { return f; } } // 没有找到则返回null值 return null; } // 菜品修改 public void updateFood(Food newFood) { for (Food f : db) { // 如果旧id与新id相等,则进行替换 if (f.getFoodId().equals(newFood.getFoodId())) { // 删除原食物 db.remove(f); // 添加新食物 db.add(newFood); return; } } } // 根据菜品ID进行删除 public void deleteFoodById(String id) { for (Food f : db) { if (f.getFoodId().equals(id)) { db.remove(f); return; } } } }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% String basePath=request.getScheme()+"://"+request.getServerName() +":"+request.getServerPort()+request.getContextPath()+"/"; %> <!DOCTYPE html> <html> <head> <base href="<%=basePath%>"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>菜名查询</title> <style type="text/css"> </style> </head> <body> <center> <h1>菜名查询</h1> <form action="<%=basePath%>/SelectServlet" method="post"> <input type="hidden" name="type" value="2"> <table width="400px" border="1px" cellspacing="0px" cellpadding="0px"> <tr> <td>菜名</td> <td><input type="text" name="foodName"></td> </tr> <tr> <td colspan="2" style="text-align:center"><input type="submit" value="查询"></td> </tr> </table> </form> </center> </body> </html>
老师,有个问题就是,还是没办法根据菜名查询出来,我控制台输出了一下,就添加的菜品也都存到db中了,但是就是查询不到,我不知道哪里有问题。老师帮忙看看
3
收起
正在回答
2回答
同学可以输出一下request.getParameter("foodName")获取到的foodName是什么?
然后改造一下查询方法,运行一下
// 根据菜品名称查询菜品信息 public Food getFoodByName(String foodName) { if(!db.isEmpty()) { for(Food f : db) { System.out.println(f.getFoodName()+"==="+foodName); if(f.getFoodName().equals(foodName)){ return f; } } System.out.println("db中没找到"); return null; }else { System.out.println("db中内容为空"); return null; } }
祝学习愉快~
从网页搭建入门Java Web2018版
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星