其余功能都能实现,只有菜品删除功能不能实现,请检查代码
贴上菜品处理类代码
package com.imooc.servlet; import com.imooc.domain.FoodInfo; import java.util.*; //菜品信息处理类 public class FoodDaoImpl { private static final List<FoodInfo> db=new ArrayList<>(); //添加新菜品 public static void addFood(FoodInfo food) { db.add(food); } //获得所有菜品信息 public static List<FoodInfo> getAllFood(){ return db; } //根据菜品名称查询菜品信息 public static List<FoodInfo> getFoodByName(String foodName){ List<FoodInfo> result=new ArrayList<>(); for(FoodInfo food:db) { if(((String)food.getFoodName()).equals(foodName)) result.add(food); } return result; } //根据菜品id查询菜品信息 public static List<FoodInfo> getFoodById(String id){ List<FoodInfo> result=new ArrayList<>(); for(FoodInfo food:db) { if(food.getId().equals(id)) result.add(food); } return result; } //菜品修改 public static void updateFood(FoodInfo food) { for(FoodInfo fooditem:db) { if(fooditem.getId().equals(food.getId())) { fooditem.setDescription(food.getDescription()); fooditem.setFoodimage(food.getFoodimage()); fooditem.setFoodName(food.getFoodName()); fooditem.setPrice(food.getPrice()); fooditem.setTaste(food.getTaste()); } } } //根据菜品ID进行删除 public static void deleteFoodById(String id) { for(FoodInfo fooditem:db) { if(fooditem.getId().toString().equals(id)) db.remove(fooditem); } } public static void main(String[] args) { // TODO Auto-generated method stub } }
贴上deletefood servlet代码
package com.imooc.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * 菜品删除servlet */ public class FoodDeleteServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String id=request.getParameter("id"); FoodDaoImpl fooddaoimpl= new FoodDaoImpl(); fooddaoimpl.deleteFoodById(id); response.sendRedirect(request.getContextPath()+"/selectFoodList.jsp"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
贴上错误信息
正在回答
如果在循环中删除集合中的数据就会出现java.util.ConcurrentModificationException异常。解决办法是在if语句中remove后写上break语句,避免再进行进行循环遍历。
一般情况下,我们会先查询菜品是否存在,然后在循环外根据查询结果,如果存在就删除,否则给出错误提示。祝学习愉快!
谢谢老师,添加了break可以跑得通了,而且我也增加了if判断;
但是我又遇到了一个新的问题, 似乎当我按顺序添加菜品时,我只能按顺序删除,比如我添加了1号和2号菜品,我只能先删除1号,再删除2号,这让我很费解
贴上新的代码
菜品处理类
package com.imooc.servlet; import com.imooc.domain.FoodInfo; import java.util.*; //菜品信息处理类 public class FoodDaoImpl { private static final List<FoodInfo> db=new ArrayList<>(); //添加新菜品 public static void addFood(FoodInfo food) { db.add(food); } //获得所有菜品信息 public static List<FoodInfo> getAllFood(){ return db; } //根据菜品名称查询菜品信息 public static List<FoodInfo> getFoodByName(String foodName){ List<FoodInfo> result=new ArrayList<>(); for(FoodInfo food:db) { if(((String)food.getFoodName()).equals(foodName)) result.add(food); } return result; } //根据菜品id查询菜品信息 public static List<FoodInfo> getFoodById(String id){ List<FoodInfo> result=new ArrayList<>(); for(FoodInfo food:db) { if(((String)food.getId()).equals(id)) result.add(food); } return result; } //菜品修改 public static void updateFood(FoodInfo food) { for(FoodInfo fooditem:db) { if(fooditem.getId().equals(food.getId())) { fooditem.setDescription(food.getDescription()); fooditem.setFoodimage(food.getFoodimage()); fooditem.setFoodName(food.getFoodName()); fooditem.setPrice(food.getPrice()); fooditem.setTaste(food.getTaste()); } break; } } //根据菜品ID进行删除 public static void deleteFoodById(String id) { for(FoodInfo fooditem:db) { if(((String)fooditem.getId()).equals(id)) db.remove(fooditem);break; } } public static void main(String[] args) { // TODO Auto-generated method stub } }
servlet的代码
package com.imooc.servlet; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.imooc.domain.FoodInfo; /** * 菜品删除servlet */ public class FoodDeleteServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8");//先进行编码转换后再取数据 String id=(String)request.getParameter("id").trim(); List<FoodInfo> list= FoodDaoImpl.getFoodById(id); if(list.size()!=0&&list!=null) { FoodDaoImpl.deleteFoodById(id); response.sendRedirect(request.getContextPath()+"/showFoodList.jsp"); } else { request.setAttribute("msg", "不存在该菜品!"); request.getRequestDispatcher("/deleteById.jsp").forward(request,response); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.imooc.domain.FoodInfo;
/**
* 菜品删除servlet
*/
public class FoodDeleteServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");//先进行编码转换后再取数据
String id=(String)request.getParameter("id").trim();
List<FoodInfo> list= FoodDaoImpl.getFoodById(id);
if(list.size()!=0&&list!=null) {
FoodDaoImpl.deleteFoodById(id);
response.sendRedirect(request.getContextPath()+"/showFoodList.jsp");
}
else {
request.setAttribute("msg", "不存在该菜品!");
request.getRequestDispatcher("/deleteById.jsp").forward(request,response);
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
再是jsp的代码
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path; %> <!DOCTYPE html> <html> <head> <base href="<%=basePath%>"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>菜品删除(根据ID删)</title> <style type="text/css"> </style> </head> <body> <center> <h1>菜品删除(根据ID删除)</h1> <% String msg=""; if(request.getAttribute("msg")!=null) { msg=(String)request.getAttribute("msg"); } %> <h3><font color="red" align="center"><%= msg %></font></h3> <form action="<%=basePath%>/FoodDeleteServlet" method="post"> <table width="400px" border="1px" cellspacing="0px" cellpadding="0px"> <tr> <td>菜品ID</td> <td><input type="text" name="id"></td> </tr> <tr> <td colspan="2" style="text-align:center"><input type="submit" value="删除"></td> </tr> </table> </form> </center> </body> </html>
补充一下跳转后的jsp的代码我觉得排除了返回的list为空的情况
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@page import="com.imooc.domain.FoodInfo" %> <%@page import="java.util.*" %> <%@page import="com.imooc.servlet.FoodDaoImpl" %> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path; %> <!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> <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> <%if(FoodDaoImpl.getAllFood()!=null&&FoodDaoImpl.getAllFood().size()!=0){ List<FoodInfo> list=FoodDaoImpl.getAllFood(); for(FoodInfo food:list){ //获得绝对路径最后一个/的位置 int lastindex=food.getFoodimage().lastIndexOf("/"); //获得文件上传时的唯一文件名 String fileName=food.getFoodimage().substring(lastindex+1); //System.out.println(fileName); %> <tr> <td><%=food.getId() %></td> <td><%=food.getFoodName() %></td> <td><%=food.getTaste() %></td> <td><img src="<%=basePath %>/upload/<%=fileName %>" /></td> <td><%=food.getPrice() %></td> <td><%=food.getDescription() %></td> </tr> <% } }else{ %> <tr> <td><a href="<%=basePath %>/addFood.jsp">请先添加菜品!</a></td> </tr> <% } %> </tbody> </table> </center> </body> </html>
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星