为何已添加的菜品里面,没有id这个数据
这很匪夷所思。我在修改菜品信息时,一直使用id是否相同进行匹配和菜品信息的修改。
可是我debug出来的发现,我的前后菜品ID是1,可是查询已添加的菜品时,里面居然没有ID这个数据??
不可能啊,我添加菜品时是有的。
添加:
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.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; /** * 菜品添加的Servlet */ @WebServlet("/FoodAddServlet") public class FoodAddServlet extends HttpServlet { List<Food> foodList = new ArrayList<Food>(); protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 数据的接收 // 文件上传基本操作: try { //定义一个Map集合用于保存接收到的数据: Map<String, String> map = 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"); map.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> foodList = (List<Food>)this.getServletContext().getAttribute("list"); // // 校验用户名: // for(Food f:foodList) { // if(f.getFoodName().equals(map.get("foodName"))) { // request.setAttribute("msg", "此菜品已存在"); // request.getRequestDispatcher("/server.jsp").forward(request,response); // return ; // } // } // 封装数据到food当中: Food food = new Food(); food.setId(Integer.parseInt(map.get("id"))); food.setFoodName(map.get("foodName")); food.setTaste(map.get("taste")); // food.setFoodImage(map.get("foodImage")); food.setFoodImage(foodImage); food.setPrice(Double.parseDouble(map.get("price"))); food.setDescription(map.get("description")); // 将添加的菜品信息存入到List集合中 foodList.add(food); for(Food f:foodList) { System.out.println(f); } this.getServletContext().setAttribute("list", foodList); // 菜品添加成功,跳转到菜品查询页面: // 存入菜品信息: // request.getSession().setAttribute("id", food.getId()); // 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); } }
修改:
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); request.getSession().setAttribute("list", list2); response.sendRedirect("/homework2/showFoodList.jsp"); return; } System.out.println(food); } } catch (FileUploadException e) { e.printStackTrace(); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
0
收起
正在回答
2回答
同学Map里面获取到的是String类型,food.getId()获取的是数字类型,这样会出错呢,同学把food.getId()转为String类型就可以了
祝学习愉快~
从网页搭建入门Java Web2018版
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星