老师,菜名查询出现问题
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 55 56 57 | package com.imooc.dish; import java.util.ArrayList; import java.util.List; import javax.security.auth.message.callback.PrivateKeyCallback.Request; public class FoodDaoImpl { //存放菜品信息的list private static final List< Food > db = new ArrayList< Food >(); private String msg; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public static List< Food > getDb() { return db; } //添加菜品 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) { for(Food f:db) { if(f.getFoodName().equals("foodName")) { return f; } } return null; } //根据菜品id查询菜品信息 public Food getFoodById(String id) { return null; } //菜品修改 public void updateFood(Food newFood) { } //根据菜品ID进行删除 public void deleteFoodById(String id) { } } |
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 | package com.imooc.servlet; import java.io.IOException; 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.dish.Food; import com.imooc.dish.FoodDaoImpl; /** * Servlet implementation class SelectServlet */ @WebServlet ( "/SelectServlet" ) public class SelectServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String foodName = "" ; Food food = new Food(); foodName = request.getParameter( "foodName" ); FoodDaoImpl fdi = new FoodDaoImpl(); food = fdi.getFoodByName(foodName); if (food == null ) { return ; } else { request.getSession().setAttribute( "food" ,food); System.out.println(food); response.sendRedirect(request.getContextPath()+ "/showFoodByName.jsp" ); } } /** * @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); } } |
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 | <% @page import = "com.imooc.dish.*" %> <%@ page language= "java" contentType= "text/html; charset=UTF-8" pageEncoding= "UTF-8" import = "java.util.*" %> <% 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> <% Food food = new Food(); if (session.getAttribute( "food" )!= null ){ food = (Food)session.getAttribute( "food" ); } %> <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.getFoodId() %></td> <td><%=food.getFoodName() %></td> <td><%=food.getTaste() %></td> <td><img src= "dish_system/WebContent/upload/<%=food.getUrl()%>" ></td> <td><%=food.getPrice() %></td> <td><%=food.getDescribe() %></td> </tr> </tbody> </table> </center> </body> </html> |
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 | <%@ 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 > |
老师,我菜名查询的时候最后是空白。
2
收起
正在回答 回答被采纳积分+1
2回答
好帮手慕小班
2019-09-29 10:45:04
同学你好,1、这里查询最后内容为空,控制台是否有报错。
2、同学在/SelectServlet中else语句里的输出food的语句,是否在控制台正确输出了呐,如果没有food内容的输出,那就是 food = fdi.getFoodByName(foodName);没有得到返回值,food为null,没有执行下面的语句。
3、如果在/SelectServlet中正确的输出了food,那说明已经正确查询到了food对象,那在对应的/showFoodByName.jsp中尝试输出获取到的food对象,在浏览器页面的控制台下中来查看是否有报错,根据报错来定位问题呐。
所以这里建议同学根据上面的步骤来确定是哪一步骤没有查到这个food对象。
如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~
从网页搭建入门Java Web2018版
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10204 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧