老师,菜名查询出现问题

老师,菜名查询出现问题

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>

老师,我菜名查询的时候最后是空白。

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

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

2回答
好帮手慕柯南 2019-09-30 10:26:54

同学你好!

同学是指food的值为null吗?同学这里为null,直接return;所以页面为空白呢

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

如果为null,同学需要跳转至页面,不能直接return ;

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

  • 提问者 Levinson #1
    老师。在SelectServlet中改了一下代码: Food food = new Food(); //获得查询菜品的名字 String foodName = request.getParameter("foodName"); FoodDaoImpl fdi = (FoodDaoImpl) request.getAttribute("fdi"); // System.out.println(foodName); food = fdi.getFoodByName(foodName); if(food == null) { response.sendRedirect(request.getContextPath()+"/addFood.jsp"); }else { request.getSession().setAttribute("food",food); response.sendRedirect(request.getContextPath()+"/showFoodByName.jsp"); } 但是他给我报了空指针异常: 严重: Servlet.service() for servlet [com.imooc.servlet.SelectServlet] in context with path [/dish_system] threw exception java.lang.NullPointerException at com.imooc.servlet.SelectServlet.doGet(SelectServlet.java:29) at com.imooc.servlet.SelectServlet.doPost(SelectServlet.java:44) food的没有传进来。不知道是为什么。老师帮忙改一下。
    2019-10-02 14:20:09
  • 好帮手慕阿莹 回复 提问者 Levinson #2
    同学你好,同学的代码中 com.imooc.servlet.SelectServlet.doGet(SelectServlet.java:29) at com.imooc.servlet.SelectServlet.doPost(SelectServlet.java:44) 这个报错的位置是哪行代码呢?请同学告知一下具体的代码哦,并且,请同学下次贴代码不要贴的回复框中呦,可以在“我要回答”中贴一下,否则会使其代码的格式。如果我的回答解决了你的疑问,请采纳,祝学习愉快。
    2019-10-06 18:43:17
好帮手慕小班 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对象。

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

  • 提问者 Levinson #1
    老师。控制台没有报错。但是如果food没有接收到对象的话,应该会有显示的画面把,里面的值为null
    2019-09-29 20:43:47
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

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

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

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

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

帮助反馈 APP下载

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

公众号

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

在线咨询

领取优惠

免费试听

领取大纲

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