麻烦老师帮我改一下,一头雾水
package com.imooc.domain; public class Food { private String id; private String name; private String taste; private String path; private String price; private String describe; public Food() { } public Food(String id, String name, String taste, String path,String price, String describe) { super(); this.id = id; this.name = name; this.taste = taste; this.path = path; this.price = price; this.describe = describe; } /** * @return the id */ public String getId() { return id; } /** * @param id the id to set */ public void setId(String id) { this.id = id; } /** * @return the name */ public String getName() { return name; } /** * @param name the name to set */ public void setName(String name) { this.name = name; } /** * @return the taste */ public String getTaste() { return taste; } /** * @param taste the taste to set */ public void setTaste(String taste) { this.taste = taste; } /** * @return the path */ public String getPath() { return path; } /** * @param path the path to set */ public void setPath(String path) { this.path = path; } /** * @return the price */ public String getPrice() { return price; } /** * @param price the price to set */ public void setPrice(String price) { this.price=price; } /** * @return the describe */ public String getDescribe() { return describe; } /** * @param describe the describe to set */ public void setDescribe(String describe) { this.describe = describe; } @Override public String toString() { return "Food [id=" + id + ", name=" + name + ", taste=" + taste + ", path=" + path + ", price=" + price + ", describe=" + describe + "]"; } } package com.imooc.domain; import java.util.ArrayList; import java.util.Iterator; import java.util.List; //菜品数据处理 public class FoodDaoImpl { //存放菜品信息的列表 private static final List<Food> db=new ArrayList<Food>(); private String msg; /** * @param msg the msg to set */ public void setMsg(String msg) { this.msg = msg; } /** * @return the msg */ public String getMsg() { return msg; } /** * @return the db */ public static List<Food> getDb() { return db; } //添加菜品 public void addFood(Food food) { db.add(food); } //查询所有菜品信息 public List<Food> getAllFood(){ if(db.isEmpty()) { System.out.println("菜单是空的"); return null; }else { return db; } } //根据菜名查询菜品信息 public Food getFoodByName(String foodName) { Food food=new Food(); for(int i=0;i<db.size();i++) { if(db.get(i).getName().equals(foodName)) { food=db.get(i); break; } } return food; } //根据菜品id查询菜品信息 public Food getFoodById(String id) { Food food=new Food(); for(int i=0;i<db.size();i++) { if(db.get(i).getId().equals(id)) { food=db.get(i); break; } } return food; } //菜品修改 public void updateFood(Food newFood) { for(int i=0;i<db.size();i++) { if(db.get(i).getId().equals(newFood.getId())) { db.remove(i); db.add(newFood); System.out.println("菜品以更新完成!"); break;//跳出循环 } } } //根据菜品id进行删除 public void deleteFoodById(String id) { if(db.isEmpty()) { System.out.println("菜单是空的!"); return;//return使整个函数返回,后面的不管是循环里面还是循环外面的都不执行 }else { for(int i=0;i<db.size();i++) { if(db.get(i).getId().equals(id)) { db.remove(i); break; } } } } } 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.domain.Food; import com.imooc.domain.FoodDaoImpl; import com.imooc.util.UploadUtils; /** * 添加菜品Servlet implementation class FoodAddServlet */ public class FoodAddServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { FoodDaoImpl FoodDaoImpl1 = (FoodDaoImpl) this.getServletContext().getAttribute("FoodDaoImpl"); List<Food> foodList = (List<Food>) this.getServletContext().getAttribute("foodlist"); if (foodList.isEmpty()) { System.out.println("从servletContext中获得的集合是空的"); } // 定义一个Map集合用于保存接收到的数据 Map<String, String> map = new HashMap<String, String>(); try { // 创建一个磁盘文件项工厂对象 DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory(); // 2创建一个核心的解析类 ServletFileUpload servletFileUpload = new ServletFileUpload(diskFileItemFactory); // 3、解析request请求,返回List集合,集合中存放的是FileItem对象 List<FileItem> list=servletFileUpload.parseRequest(request); // 4.遍历list,查看传递过来的数据是什么 String url=null; for(FileItem fileItem:list) { if(fileItem.isFormField()) { //普通表单项 //接收表单项参数的值 String name=fileItem.getFieldName(); String value=fileItem.getString("UTF-8"); //存放在map中 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(); } } } if(foodList==null) { System.out.println("从ContextServlet中获得的菜单是空的"); if(foodList!=null) { //检验网页上添加的菜名是否存在 for(Food food:foodList) { if(food.getId().equals(map.get("id"))) { request.setAttribute("msg", "菜名已存在"); //菜名已存在,跳转到添加界面 request.getRequestDispatcher("/addFood.jsp").forward(request, response); return; } } } //新添加的菜名可上传,封装相关数据 Food food=new Food(); food.setId(map.get("id")); food.setName(map.get("foodName")); food.setTaste(map.get("taste")); food.setPath(url); food.setPrice(map.get("price")); food.setDescribe(map.get("description")); if(FoodDaoImpl1==null) { FoodDaoImpl1=new FoodDaoImpl(); //添加到全局对象FoodDaoImp1中 FoodDaoImpl1.addFood(food); this.getServletContext().setAttribute("FoodDaoImpl1", FoodDaoImpl1); //跳转到展示食物界面 request.getRequestDispatcher("/showFoodList.jsp").forward(request, response); } } } catch (FileUploadException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 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.Food; import com.imooc.domain.FoodDaoImpl; /** * Servlet implementation class FoodDeleteServlet */ public class FoodDeleteServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public FoodDeleteServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { FoodDaoImpl FoodDaoImpl1=(FoodDaoImpl) this.getServletContext().getAttribute("FoodImple1"); List<Food> foodList=FoodDaoImpl1.getDb(); String deleteFoodId=request.getParameter("id"); String msg=FoodDaoImpl1.getMsg(); boolean flag=false; for(Food food:foodList) { if(food.getId().equals(deleteFoodId)) { flag=true; } if(flag==false) { msg="输入的id不存在,请重新输入"; request.setAttribute("msg", msg); }else { FoodDaoImpl1.deleteFoodById(deleteFoodId); this.getServletContext().setAttribute("FoodImpl1", FoodDaoImpl1); msg="根据id删除菜品已完成"; request.setAttribute("msg", msg); request.getRequestDispatcher("/showFoodList.jsp").forward(request, response); } } } } 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.Iterator; 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.domain.Food; import com.imooc.domain.FoodDaoImpl; import com.imooc.util.UploadUtils; /** * Servlet implementation class FoodUpdateServlet */ public class FoodUpdateServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public FoodUpdateServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); FoodDaoImpl FoodDaoImpl1=(FoodDaoImpl) this.getServletContext().getAttribute("FoodDaoImpl"); List<Food> foodList=FoodDaoImpl1.getDb(); if(foodList.isEmpty()) { System.out.println("从ServletContext中获得的list是空的"); } Map<String,String> map=new HashMap<String,String>(); try { //1创建磁盘拱辰享文件 DiskFileItemFactory diskFileItemFactory=new DiskFileItemFactory(); //2创建一个核心解析类 ServletFileUpload servletFileUpload=new ServletFileUpload(diskFileItemFactory); //3解析request请求,返回的是List集合,List集合中存放的是FileItem对象 List<FileItem> list=servletFileUpload.parseRequest(request); //4遍历List集合,判断FileItem类型 String url=null; for(FileItem fileItem:list) { if(fileItem.isFormField()) { String name=fileItem.getName(); String value=fileItem.getString(); 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("/img"); 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(); } } } //检验菜名 Iterator<Food> iterator=foodList.iterator(); boolean flag=false; Food testFood=null; while(iterator.hasNext()) { testFood=iterator.next(); if(testFood.getId().equals(map.get("id"))) { flag=true; break; } } if(flag==false) { String msg="您要修改的id:"+map.get("id")+"菜单里没有"; request.setAttribute("msg", msg); request.getRequestDispatcher("/updateFood.jsp").forward(request, response); }else { Food food=new Food(); food.setId(map.get("id")); food.setName(map.get("name")); food.setTaste(map.get("taste")); food.setPath(url); food.setPrice(map.get("price")); food.setDescribe(map.get("describe")); FoodDaoImpl1.updateFood(food); System.out.println("更新后的food为"+food); this.getServletContext().setAttribute("FoodDaoImpl1", FoodDaoImpl1); request.getRequestDispatcher("/showFood.jsp").forward(request, response); } } catch (FileUploadException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 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.Food; import com.imooc.domain.FoodDaoImpl; /** * 初始化Servlet implementation class InitServlet */ public class InitServlet extends HttpServlet { @Override public void init() throws ServletException { //创建FoodDaoImpl对象 FoodDaoImpl FoodDaoImpl=new FoodDaoImpl(); //创建一个list集合用来接收DaoImpl中的list List<Food> foodList=FoodDaoImpl.getDb(); //将初始化的FoodDaoImpl对象及其list表存放到ServletContext中 this.getServletContext().setAttribute("FoodDaoImpl1", FoodDaoImpl); this.getServletContext().setAttribute("foodList", foodList); } } 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.Food; import com.imooc.domain.FoodDaoImpl; /** * Servlet implementation class SelectServlet */ public class SelectServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public SelectServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); FoodDaoImpl FoodDaoImpl1=(FoodDaoImpl) this.getServletContext().getAttribute("FoodDaoImpl"); List<Food> list=FoodDaoImpl1.getDb(); String foodName=request.getParameter("foodName"); Food foodSearch=FoodDaoImpl1.getFoodByName(foodName); boolean flag=false; if(foodSearch!=null) { flag=true; } if(flag==false) { String msg="对不起,您要查找的"+foodName+"在我们家菜单没有"; request.setAttribute("msg", msg); request.getRequestDispatcher("/selectFoodByName.jsp").forward(request, response); }else { request.getSession().setAttribute("foodSearch", foodSearch); request.getRequestDispatcher("/showFoodByName.jsp").forward(request, response); } } } package com.imooc.util; import java.util.UUID; public class UploadUtils { /** * 生成唯一文件名 */ public static String getUUIDFileName(String fileName) { //将文件名前面部分进行截取 int idx=fileName.lastIndexOf("."); String extention=fileName.substring(idx); String uuidFileName=UUID.randomUUID().toString().replace("-", "")+extention; return uuidFileName; } } <%@ page contentType="text/html;charset=utf-8"%> <!DOCTYPE html> <html> <head> <% String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath(); %> <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> <% String msg=""; if(request.getAttribute("msg")!=null){ msg=(String)request.getAttribute("msg"); } %> <h2 style="color:green;font-weight:bolder;">${requestScope.msg }</h2> <form action="<%=basePath%>/add" 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 contentType="text/html;charset=utf-8"%> <!DOCTYPE html> <html> <head> <% String menu=request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath(); %> <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> <h2 style="color:red;font-weight:bolder;">${requestScope.msg}</h2> <form action="<%=basePath%>/delete" 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> <%@ page contentType="text/html;charset=utf-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <p><a href="addFood.html" target="main">菜品添加</a></p> <p><a href="selectFood.html" target="main">菜品查询</a></p> <p><a href="updateFood.html" target="main">菜品修改</a></p> <p><a href="deleteById.html" target="main">菜品删除</a></p> </body> </html> <%@ page contentType="text/html;charset=utf-8"%> <!DOCTYPE html> <html> <head> <% String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath(); %> <base href="<%=basePath%>"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>菜品查询选项页面</title> <style type="text/css"> </style> </head> <body> <center> <p><a href="/showFoodList.jsp">查询所有菜品信息</a></p> <p><a href="/selectFoodByName.jsp">菜名查询</a></p> </center> </body> </html> <%@ page contentType="text/html;charset=utf-8"%> <!DOCTYPE html> <html> <head> <% String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath(); %> <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> <% String msg=""; if(request.getAttribute("msg")!=null){ msg=(String)request.getAttribute("msg"); } %> <form action="<%=basePath%>/select" 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="foodName"></td> </tr> <tr> <td colspan="2" style="text-align:center"><input type="submit" value="查询"></td> </tr> </table> </form> </center> </body> </html> <%@ page import=java.* %> <%@ page contentType="text/html;charset=utf-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>菜品管理系统</title> </head> <frameset rows="20%,*"> <frame src="./top.html"></frame> <frameset cols="10%,*"> <frame src="./left.html"></frame> <frame name="main"></frame> </frameset> </frameset> </html> <%@ import=java.* %> <%@ page contentType="text/html;charset=utf-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!DOCTYPE html> <html> <head> <% String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath(); %> <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> <c:forEach items="${applicationScope.FoodDaoImpl1.getDb() }" var="food"> <tr> <td>${food.Id }</td> <td>${food.name }</td> <td>${food.taste }</td> <td><img src=${food.path}></td> <td>${food.price }</td> <td>${food.describe }</td> </tr> </c:forEach> </tbody> </table> </center> </body> </html> <%@ page contentType="text/html;charset=utf-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <center> <h1>菜品后台管理系统</h1> </center> </body> </html> <%@ page contentType="text/html;charset=utf-8"%> <!DOCTYPE html> <html> <head> <% String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath(); %> <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> <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> 麻烦帮我改好,谢谢
4
收起
正在回答 回答被采纳积分+1
1回答
从网页搭建入门Java Web2018版
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星