菜品修改有问题
当列表里多个菜品信息,菜品修改后不能跳转到显示菜品信息界面,但是再去查询的时候发现修改成功;当列表里只有一条菜品信息时,就能跳转成功,感觉好迷呀
//菜品修改 public static void updateFood(Map<String,Food> map) { //boolean flag=true; Map<String,Food> map1 = null; for(Map<String,Food> m : db) { for(String key:m.keySet()){ for(String k:map.keySet()) { if(map.get(k).getId().equals(m.get(key).getId())) { map1 = m; break; } } } db.remove(map1); db.add(map); } }
package com.imooc.servlet; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; import com.imooc.food.Food; import com.imooc.food.FoodDaoImpl; import com.imooc.utils.UploadUtils; /** * 菜品修改的Servlet */ public class FoodUpdateServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 文件上传的基本操作 try { //定义一个map集合用于保存接收到的数据 Map<String,String> map = new HashMap<String,String>(); //定义一个map集合用于保存菜品信息 Map<String,Food> foodMap = new HashMap<String,Food>(); // 1、创建一个磁盘文件项工厂对象 DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory(); // 2、创建一个核心解析类 ServletFileUpload servletFileUpload = new ServletFileUpload(diskFileItemFactory); // 3、解析request请求,返回list集合,list中存放FileItem对象 List<FileItem> list = servletFileUpload.parseRequest(request); // 4、遍历集合,获得FileItem,判断是普通表单项还是文件上传项 String url=null; for(FileItem fileItem:list) { if(fileItem.isFormField()) { //普通表单项 String name = fileItem.getFieldName(); String value = fileItem.getString("utf-8"); System.out.println(name+" "+value); map.put(name, value); }else { //文件上传项 //获得文件上传的名称 String fileName = fileItem.getName(); if(fileName!=null&& !"".equals(fileName)) { //获得唯一的文件名 String uuidFileName = UploadUtils.getUUIDFileName(fileName); //获得文件上传的数据 InputStream is = fileItem.getInputStream(); //获得路径 String path = this.getServletContext().getRealPath("/upload"); //将输入流对接到输出流 url = path+"//"+uuidFileName; OutputStream os = new FileOutputStream(url); int len=0; byte[] b=new byte[1024]; while((len=is.read(b))!=-1) { os.write(b, 0, len); } is.close(); os.close(); } } } //获得ServletContext对象 //FoodDaoImpl foodDaoImpl = new FoodDaoImpl(); //List<Map<String,Food>> foodList = (List<Map<String, Food>>) this.getServletContext().getAttribute("list"); List<Map<String,Food>> foodList = FoodDaoImpl.getAllFood(); //校验菜品id是否存在 List<Map<String,Food>> idList = FoodDaoImpl.getFoodById(map.get("id")); if(idList.size()!=0) { //将数据封装到food类中 Food food = new Food(); food.setId(map.get("id")); food.setFoodName(map.get("foodName")); food.setTaste(map.get("taste")); food.setPrice(map.get("price")); food.setDescription(map.get("description")); food.setPath(url); //将菜品信息存入到list集合中 foodMap.put(food.getId(), food); //foodDaoImpl.addFood(foodMap); FoodDaoImpl.updateFood(foodMap); //this.getServletContext().setAttribute("list", foodList); //添加成功,跳转到显示菜品信息页面 System.out.println("修改成功!"); //request.getSession().setAttribute("foodList", foodList); response.sendRedirect(request.getContextPath()+"/showFoodList.jsp"); }else { request.setAttribute("msg", "该菜品id不存在!"); request.getRequestDispatcher("updateFood.jsp").forward(request, response); } } catch (FileUploadException e) { // TODO Auto-generated catch block e.printStackTrace(); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
<%@ 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> <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"><%= msg %></font></h3> <form action="<%= basePath %>/FoodUpdateServlet" method="post" enctype="multipart/form-data"> <table border="1px" width="400px" cellspacing="0px" cellpadding="0px"> <tr> <td>修改ID</td> <td><input type="text" name="id"></td> </tr> <tr> <td>菜 名</td> <td><input type="text" name="foodName"></td> </tr> <tr> <td>口 味</td> <td> <input type="radio" name="taste" value="香辣">香辣 <input type="radio" name="taste" value="微辣">微辣 <input type="radio" name="taste" value="麻辣">麻辣 <input type="radio" name="taste" value="不辣">不辣 </td> </tr> <tr> <td>菜品图片</td> <td><input type="file" name="foodImage"></td> </tr> <tr> <td>价 格</td> <td><input type="text" name="price"></td> </tr> <tr> <td>菜品描述</td> <td> <textarea name="description"></textarea> </td> </tr> <tr style="text-align:center;width:20px"> <td colspan="2"> <input type="submit" value="修改"> <input type="reset" value="重置"> </td> </tr> </table> </form> </center> </body> </html>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" import="java.util.*,com.imooc.food.*"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path; %> <!DOCTYPE html> <html> <head> <base href="/immocPro2"> <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> <% //FoodDaoImpl foodDaoImpl = new FoodDaoImpl(); //List<Map<String, Food>> list = (List<Map<String, Food>>)session.getAttribute("foodList"); List<Map<String, Food>> list = FoodDaoImpl.getAllFood(); if (list.size() != 0) { for (Map<String, Food> map : list) { for (String key : map.keySet()) { int idx = map.get(key).getPath().lastIndexOf("//"); String fileName = map.get(key).getPath().substring(idx + 1); %> <tbody> <tr> <td><%=map.get(key).getId()%></td> <td><%=map.get(key).getFoodName()%></td> <td><%=map.get(key).getTaste()%></td> <td><img src="<%=basePath%>/upload/<%=fileName%>" /></td> <td><%=map.get(key).getPrice()%></td> <td><%=map.get(key).getDescription()%></td> </tr> <% } } } else { %> <h3> <font color="red">当前列表为空!</font> </h3> <% } %> </tbody> </table> </center> </body> </html>
0
收起
正在回答
5回答
这是错误500的异常哦,500错误是代码出现了问题,错误原因是你在for循环中移除菜品信息了,所以会报错,建议你定义一个boolean类型的flag,默认值为false,当找到菜品信息时,将flag设置为true,然后在for循环外根据flag的值进行操作,祝学习愉快~
慕桂英8566455
2018-10-09 15:44:54
为什么我自己电脑上就不行啊,如图:
而且后台也没有打印出“修改成功!”,可是再去菜品查询的时候发现信息已经被修改了
chrismorgen
2018-10-09 15:18:07
你好同学,测试了你的代码,当列表中存在多个菜品信息时,在修改菜品信息时,是可以跳转到菜品信息查询页面的,下图是测试之后的结果,祝学习愉快~
慕桂英8566455
2018-10-09 14:21:50
package com.imooc.food; public class Food { private String id; private String foodName; private String taste; private String path; private String price; private String description; public Food() { } public Food(String id, String foodName, String taste, String path, String price, String description) { super(); this.id = id; this.foodName = foodName; this.taste = taste; this.path = path; this.price = price; this.description = description; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getFoodName() { return foodName; } public void setFoodName(String foodName) { this.foodName = foodName; } public String getTaste() { return taste; } public void setTaste(String taste) { this.taste = taste; } public String getPath() { return path; } public void setPath(String path) { this.path = path; } public String getPrice() { return price; } public void setPrice(String price) { this.price = price; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } @Override public String toString() { return "Food [id=" + id + ", foodName=" + foodName + ", taste=" + taste + ", path=" + path + ", price=" + price + ", description=" + description + "]"; } }
package com.imooc.food; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class FoodDaoImpl { //存放菜品信息的List private static final List<Map<String,Food>> db = new ArrayList<Map<String,Food>>(); //添加菜品 public static void addFood(Map<String,Food> map) { db.add(map); } //查询所有菜品信息 public static List<Map<String,Food>> getAllFood(){ return db; } //根据菜品名称查询菜品信息 public static List<Map<String,Food>> getFoodByName(String name){ List<Map<String,Food>> list = new ArrayList<Map<String,Food>>(); boolean flag=false; Map<String,Food> map=null; for(Map<String,Food> l : db) { for(String key:l.keySet()) { if(l.get(key).getFoodName().equals(name)) { map=l; flag=true; break; } } } if(flag) list.add(map); return list; } //根据菜品id查询菜品信息 public static List<Map<String,Food>> getFoodById(String id){ List<Map<String,Food>> list = new ArrayList<Map<String,Food>>(); Map<String,Food> map=null; boolean flag = false; for(Map<String,Food> l : db) { for(String key:l.keySet()) { if(l.get(key).getId().equals(id)) { map=l; flag=true; break; } } } if(flag) list.add(map); return list; } //菜品修改 public static void updateFood(Map<String,Food> map) { //boolean flag=true; Map<String,Food> map1 = null; for(Map<String,Food> m : db) { for(String key:m.keySet()){ for(String k:map.keySet()) { if(map.get(k).getId().equals(m.get(key).getId())) { map1 = m; break; } } } db.remove(map1); db.add(map); } } //根据菜品id进行删除 public static void deleteFoodById(String id) { Map<String,Food> map = null; for(Map<String,Food> l : db) { for(String key:l.keySet()) { if(l.get(key).getId().equals(id)) { map = l; break; } } } db.remove(map); } }
chrismorgen
2018-10-07 13:26:59
建议你把FoodUpdateServlet中的response.sendRedirect(request.getContextPath()+"/showFoodList.jsp");这段代码放到if else语句外试一试,如果我的建议解决了你的问题,请采纳,祝学习愉快~
从网页搭建入门Java Web2018版
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星