关于显示所有菜品出现500的问题!
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>菜品查询选项页面</title> <style type="text/css"> </style> </head> <body> <center> <p><a href="/manu/ShowFoodServlet">查询所有菜品信息</a></p> <p><a href="selectFoodByName.html">菜名查询</a></p> </center> </body> </html>
package 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 domain.Food; import utils.FoodDaoImpl; /** * 显示所有菜品servlet */ @WebServlet("/ShowFoodServlet") public class ShowFoodServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //实例化取全部数据方法 FoodDaoImpl foodDaoImpl=new FoodDaoImpl(); //调用方法取出所有食物,存储到Food类的List列表里 List<Food>list=foodDaoImpl.getAllFood(); //存储到request的属性中 request.setAttribute("list", list); request.getRequestDispatcher("/showFoodList.jsp").forward(request, response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>菜品信息展示</title> <style type="text/css"> </style> </head> <body> <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> <% //直接读取从servlet传过来的request数据 List<Food> foodList = (List<Food>)request.getAttribute("list"); //判断如果这个list不为空,则循环输出所有数据,如果为空则提示. for(Food food:foodList){ String id =food.getId ; String name = food.getName; String taste = food.getTaste; String path = food.getPath; String price = food.getPrice; String info = food.getInfo; %> <tr> <td><%=id %></td> <td><%= %></td> <td><%= %></td> <td><img src="<%= %>"></td> <td><%= %></td> <td><%= %></td> </tr> <% } %> </tbody> </table> </center> </body> </html>
点查询所有菜单后出现500错误!菜品可以正常添加,显示所有菜品的方法是return db;
0
收起
正在回答 回答被采纳积分+1
4回答
龍彦宏
2019-01-22 11:11:20
再補充一下,现在做完搜索菜品也是500出错,不知道哪里出了问题.补上代码.
package utils; import java.util.ArrayList; import java.util.List; import domain.Food; /** * 菜品处理类 */ public class FoodDaoImpl { //存放菜品List集合 private static final List<Food> db = new ArrayList<Food>(); //添加菜品 public void addFood(Food food){ db.add(food); } //显示所有菜品 public List<Food>getAllFood(){ return db; } //根据菜品名称查询菜品信息 public List<Food >getFoodByName(String foodName){ List<Food > foodList = new ArrayList<>();//创建一个list集合接收数据 Food getFood = null; for(Food food : db){ if(food.getName().equals(foodName)){ getFood = food; foodList.add(getFood); } } return foodList ; } }
<!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="/manu/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="name"></td> </tr> <tr> <td colspan="2" style="text-align:center"><input type="submit" value="查询"></td> </tr> </table> </form> </center> </body> </html>
package 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 domain.Food; import utils.FoodDaoImpl; /** * 通过名称查询菜品名称 */ @WebServlet("/SelectServlet") public class SelectServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //从表单获取查询名称 String foodName = request.getParameter("name"); //实例化调取命令查询对应食物集合 FoodDaoImpl foodDaoImpl = new FoodDaoImpl(); Food food = (Food) foodDaoImpl.getFoodByName(foodName); request.setAttribute("food",food); request.getRequestDispatcher("/showFoodList2.jsp").forward(request, response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } }
<%@ page import="java.util.List" %> <%@ page import="domain.Food" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>菜品信息展示</title> <style type="text/css"> </style> </head> <body> <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> <% List<Food> foodList = (List<Food>) request.getAttribute("list"); Food food = (Food)request.getAttribute("food"); if(food!=null){%> <tr> <td><%=food.getId()%></td> <td><%=food.getName()%></td> <td><%=food.getTaste()%></td> <td><img src="<%=food.getPath()%>"></td> <td><%=food.getPrice()%></td> <td><%=food.getDescription()%></td> </tr> <% }else{ for (Food food1 : foodList) {%> <tr> <td><%=food1.getId()%></td> <td><%=food1.getName()%></td> <td><%=food1.getTaste()%></td> <td><img src="<%=food1.getPath()%>"></td> <td><%=food1.getPrice()%></td> <td><%=food1.getDescription()%></td> </tr> <%} }%> </tbody> </table> </center> </body> </html>
老师...我觉得自己做这个太困难了,没有教程对这种增删改查的,可否把后面的修改和删除的方法都铁给我...这块太耽误时间了,我得赶紧学后面的了.
Java Web基础入门2018版
- 参与学习 716 人
- 提交作业 185 份
- 解答问题 1363 个
会Java?懂前端基础?想学后台开发?那么,赶快来学习《Java Web入门》路径吧。本路径主要介绍Java Web的基础知识,并配有大量案例,定会让你收获多多!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星