修改菜品信息的问题。I need U HELP !
我在这两个位置都加了断点,可是进不去debug页面是为什么?而且我修改的数据依旧没有存入我的集合进行页面展示。
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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | package homework2.servlet; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; 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 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 homework2.domain.Food; import homework2.utils.UploadUtils; /** * 菜品修改(根据菜品ID进行修改)的Servlet */ @WebServlet ( "/FoodUpdateServlet2" ) public class FoodUpdateServlet2 extends HttpServlet { List<Food> foodList = new ArrayList<Food>(); protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 数据的接收 // 文件上传基本操作: try { //定义一个Map集合用于保存接收到的数据: Map<String, String> updatemap = new HashMap<String, String>(); // 1、创建一个磁盘文件项工厂对象。 DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory(); // 2、创建一个核心解析类。 ServletFileUpload servletFileUpload = new ServletFileUpload(diskFileItemFactory); // 3、解析request请求,返回的是List集合,其中存放的是FileItem对象。 List<FileItem> list = servletFileUpload.parseRequest(request); // 4、遍历集合,获得每个FileItem,判断表单项还是文件上传项。 String foodImage = null ; for (FileItem fileItem:list) { // 判断是表单项还是文件上传项 if (fileItem.isFormField()) { // 普通表单项:接收表单项参数的值: // 获得表单的name属性的值 String name = fileItem.getFieldName(); // 获得表单项的值 String value = fileItem.getString( "UTF-8" ); updatemap.put(name, value); System.out.println( "接收到的:" +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" ); // 将输入流对接到输出流就可以了 foodImage = path+ "\\" +uuidFileName; OutputStream os = new FileOutputStream(foodImage); int len = 0 ; byte [] b = new byte [ 1024 ]; while ((len = is.read(b))!=- 1 ) { os.write(b, 0 , len); } is.close(); os.close(); } } } // 从ServletContext域中获得保存菜品名称信息的集合: List<Food> list2 = (List<Food>) this .getServletContext().getAttribute( "list" ); for (Food food:list2) { // 判断id是否相同 if (updatemap.get( "id" ).equals(food.getId())) { // 查询成功: food.setFoodName(updatemap.get( "foodName" )); food.setTaste(updatemap.get( "taste" )); food.setFoodImage(foodImage); food.setPrice(Double.parseDouble(updatemap.get( "price" ))); food.setDescription(updatemap.get( "description" )); System.out.println(food); // list2.add(food); // request.getSession().setAttribute("foodName", food.getFoodName()); // request.getSession().setAttribute("taste", food.getTaste()); // request.getSession().setAttribute("foodImage", food.getFoodImage()); // request.getSession().setAttribute("price", food.getPrice()); // request.getSession().setAttribute("description", food.getDescription()); // request.getSession().setAttribute("foodList", foodList); request.getSession().setAttribute( "list" , list2); response.sendRedirect( "/homework2/showFoodList.jsp" ); return ; } System.out.println(food); } // 封装数据到food当中: // Food food = new Food(); // food.setId(Integer.parseInt(updatemap.get("id"))); // food.setFoodName(updatemap.get("foodName")); // food.setTaste(updatemap.get("taste")); // food.setFoodImage(foodImage); // food.setPrice(Double.parseDouble(updatemap.get("price"))); // food.setDescription(updatemap.get("description")); // 将添加的菜品信息存入到List集合中 // foodList.add(food); // for(Food f:foodList) { // System.out.println(f); // } // this.getServletContext().setAttribute("list", foodList); // 菜品添加成功,跳转到菜品查询页面: // 存入菜品信息: // request.getSession().setAttribute("foodName", food.getFoodName()); // request.getSession().setAttribute("taste", food.getTaste()); // request.getSession().setAttribute("foodImage", food.getFoodImage()); // request.getSession().setAttribute("price", food.getPrice()); // request.getSession().setAttribute("description", food.getDescription()); // // request.getSession().setAttribute("foodList", foodList); // response.sendRedirect(request.getContextPath()+"/showFoodList.jsp"); } catch (FileUploadException e) { e.printStackTrace(); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } |
请问是哪里出了问题。感觉快出来了,可是就是看不到希望之光,一直埋在阴霾里煎熬
1
收起
正在回答 回答被采纳积分+1
1回答
好帮手慕柯南
2019-05-09 10:57:35
同学你好!
1,断点问题:同学的断点打在了for循环里面的if语句中没有反应,这说明同学的if语句没有进去,同学可以在for循环的外面打断点,看一个if语句为什么没有进去
2.还有一点不知道同学为什么是ServletContext域中获得保存菜品名称信息的集合,最后又将数据保存在了seesion中。这样ServletContext域中的数据并没变化,不知道同学的seesion和ServletContext具体会在什么时候用,如果同学的ServletContext中是用来存在所有的菜品的,修改之后要更新哦~同学在页面显示时也要分清楚要用哪一个。
同学最后的保存数据和转发页面可以写在for循环的外面呢。
3.最后马上就是光明了呢~
如果我的回答解决了你的疑惑,请采纳,祝学习愉快~
从网页搭建入门Java Web2018版
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10204 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧