为什么我的查询,修改,删除功能都实现不了?

来源:5-2 作业题

造梦大师

2019-03-28 16:40:58

  • http://img.mukewang.com/climg/5c9c883c0001e47804810877.jpg

  • package com.imooc.food;

  • public class Food {
     private String id;
     private String name;
     private String taste;
     private String foodAdress;
     private String price;
     private String describe;
     public Food() {
      super();
      // TODO Auto-generated constructor stub
     }
     public Food(String id, String name, String test, String foodAdress, String price, String describe) {
      super();
      this.id = id;
      this.name = name;
      this.taste = taste;
      this.foodAdress = foodAdress;
      this.price = price;
      this.describe = describe;
     }
     public String getId() {
      return id;
     }
     public void setId(String id) {
      this.id = id;
     }
     public String getName() {
      return name;
     }
     public void setName(String name) {
      this.name = name;
     }
     public String getTaste() {
      return taste;
     }
     public void setTaste(String test) {
      this.taste = test;
     }
     public String getFoodAdress() {
      return foodAdress;
     }
     public void setFoodAdress(String foodAdress) {
      this.foodAdress = foodAdress;
     }
     public String getPrice() {
      return price;
     }
     public void setPrice(String price) {
      this.price = price;
     }
     public String getDescribe() {
      return describe;
     }
     public void setDescribe(String describe) {
      this.describe = describe;
     }
     @Override
     public String toString() {
      return "菜品的信息: id=" + id + ", name=" + name + ", taste=" + taste + ", foodAdress=" + foodAdress + ", price="
        + price + ", describe=" + describe ;
     }
     
     
     
    }

  • package com.imooc.food;

  • import java.util.ArrayList;
    import java.util.List;

  • public class FoodDaolmpl {
     //全局变量:bd用来储存菜品信息(应该只有一个,所以只能创建一个FoodDaolmpl对象)
     private static final List<Food> db=new ArrayList();
     
     public static void addFood(Food food){
        db.add(food);
     }
     
     public static List<Food> getAllFood(){
      return db;
     }
     
     public static Food getFoodByName(String foodName){
      for (Food food : db) {
       if(foodName.equals(food.getName())){
        return food;
       }
      }
      return null;
     }
     
     public static Food getFoodById(String foodId){
      for (Food food : db) {
       if(foodId.equals(food.getId())){
        return food;
       }
      }
      return null;
     }
     
     public static void updateFood(Food newFood){
      for (Food food : db) {
       if(food.getId().equals(newFood.getId())){
        db.remove(food);
        db.add(newFood);
       }
      }
     }
     
     public static void deleteFoodById(String id){
      Food o=getFoodById(id);
      db.remove(o);
     }
    }

  • package com.imooc.food.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 com.imooc.food.Food;
    import com.imooc.food.FoodDaolmpl;
    import com.imooc.food.utiles.UploadFileData;
    import com.imooc.food.utiles.UploadUtils;

  • /**
     * 菜品添加的Servlet
     */
    @WebServlet("/FoodAddServlet")
    public class FoodAddServlet extends HttpServlet {
     protected void doGet(HttpServletRequest request, HttpServletResponse response)
       throws ServletException, IOException {
      // 数据的接收
      Food food = UploadFileData.UploadFile(request);
      List<Food> foodList =FoodDaolmpl.getAllFood();
      for (Food food2 : foodList) {
       if(food.getId().equals(food2.getId())){
        request.setAttribute("msg", "此菜品id已存在!");
        request.getRequestDispatcher("/addFood.jsp").forward(request, response);
       }
      }
       FoodDaolmpl.addFood(food);
       // 添加成功,跳转到查询页面
       response.sendRedirect(request.getContextPath() + "/showFoodList.jsp");
      
     }

  •  /**
      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
      *      response)
      */
     protected void doPost(HttpServletRequest request, HttpServletResponse response)
       throws ServletException, IOException {
      // TODO Auto-generated method stub
      doGet(request, response);
     }

  • }

  • package com.imooc.food.servlet;

  • import java.io.IOException;
    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 com.imooc.food.Food;
    import com.imooc.food.FoodDaolmpl;

  • /**
     * Servlet implementation class FoodDeleteServlet
     */
    @WebServlet("/FoodDeleteServlet")
    public class FoodDeleteServlet extends HttpServlet {
     
     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      String id =request.getParameter("id");
      for (Food food : FoodDaolmpl.getAllFood()) {
       if(food.getName().equals("id")){
        FoodDaolmpl.deleteFoodById(id);
        //删除成功,重定向到showFoodList.jsp
        request.getRequestDispatcher("/showFoodList.jsp").forward(request, response);
       }
      }
       request.getRequestDispatcher("/delecteById.jsp").forward(request, response);
      
     }

  •  /**
      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
      */
     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      // TODO Auto-generated method stub
      doGet(request, response);
     }

  • }

  • package com.imooc.food.servlet;

  • import java.io.IOException;
    import java.util.List;

  • 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 com.imooc.food.Food;
    import com.imooc.food.FoodDaolmpl;
    import com.imooc.food.utiles.UploadFileData;

  • /**
     * Servlet implementation class FoodUpdateServlet
     */
    @WebServlet("/FoodUpdateServlet")
    public class FoodUpdateServlet extends HttpServlet {
     
     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      //获取数据
      Food food=UploadFileData.UploadFile(request);
      //检查id是否存在
      List<Food> foodList =FoodDaolmpl.getAllFood();
      for (Food food2 : foodList) {
       if(food.getId().equals(food2.getId())){
        FoodDaolmpl.updateFood(food);
        //修改成功,跳转到查询页面
        request.getRequestDispatcher("/showFoodList.jsp").forward(request, response);
        
       }
      }  
       request.setAttribute("msg", "此菜品id不存在!");
       request.getRequestDispatcher("/updateFood.jsp").forward(request, response);
      
      
      
     }

  •  /**
      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
      */
     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      // TODO Auto-generated method stub
      doGet(request, response);
     }

  • }

  • package com.imooc.food.servlet;

  • import java.util.ArrayList;
    import java.util.List;

  • import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;

  • import com.imooc.food.Food;
    import com.imooc.food.FoodDaolmpl;

  • /**
     * 用于菜品管理的初始化
     */
    @WebServlet(loadOnStartup=2,urlPatterns={"/InitServlet"})
    public class InitServlet extends HttpServlet {
     @Override
     public void init() throws ServletException {
      //通过getAllFood方法获得db对象
      this.getServletContext().setAttribute("list", FoodDaolmpl.getAllFood());
     }
    }

  • package com.imooc.food.servlet;

  • import java.io.IOException;
    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 com.imooc.food.Food;
    import com.imooc.food.FoodDaolmpl;

  • /**
     * Servlet implementation class SelectServlet
     */
    @WebServlet("/SelectServlet")
    public class SelectServlet extends HttpServlet {
     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      String name =request.getParameter("foodName");
      Food food =FoodDaolmpl.getFoodByName(name);
      request.setAttribute("food", food);
      
      //查询成功,返回到查询界面
      response.sendRedirect(request.getContextPath()+"/showSelectFoodList.jsp");
     }

  •  /**
      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
      */
     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      // TODO Auto-generated method stub
      doGet(request, response);
     }

  • }

  • package com.imooc.food.utiles;

  • 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.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.FoodDaolmpl;

  • public class UploadFileData {
     /**
      * 文件的数据接收类
      * @throws IOException
      */
     public static Food UploadFile(HttpServletRequest request) throws IOException{
      String url=null;
      //定义一个Map集合用于保存接收到的数据
      Map<String ,String>map =new HashMap<String ,String>();
      //创建一个磁盘文件项工厂对象
      DiskFileItemFactory diskFileItemFactory =new DiskFileItemFactory();
      //创建一个核心解析类
      ServletFileUpload servletFileUpload =new ServletFileUpload(diskFileItemFactory);
      //解析request请求,返回的是List集合,集合中存放的是FileItem对象
      List<FileItem> list;
      try {
       list = servletFileUpload.parseRequest(request);
       //遍历集合,获得每个FileItem,判断是表单项还是文件上传项
       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();
         //通过工具类获得唯一文件名
         String uuidFileName = UploadUtils.getUUIDFileName(FileName);
         //获得文件上传数据
         InputStream is = fileItem.getInputStream();
         //获得文件上传的路径
         String path =request.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();
        }
       }
       
      } catch (FileUploadException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }
      System.out.println(map);
      Food food=new Food();
      food.setId(map.get("id"));
      food.setName(map.get("foodName"));
      food.setPrice(map.get("price"));
      food.setDescribe(map.get("describe"));
      food.setTaste(map.get("taste"));
      food.setFoodAdress(url);
      return food;
      }
    }

  • package com.imooc.food.utiles;

  • import java.util.UUID;

  • public class UploadUtils {
     /**
      * 文件上传的工具类
      * @author jt
      *
      */
     public static String getUUIDFileName(String fileName){
      int idx =fileName.lastIndexOf(".");
      String extention =fileName.substring(idx);
      String UUIDName = UUID.randomUUID().toString().replaceAll("-", "");
      String UUIDFileName = UUIDName+extention;
      
      return UUIDFileName;
     }
    }

  • <!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>
      <p><a href="showAllFoodList.jsp">查询所有菜品信息</a></p>
      <p><a href="selectFoodByName.html">菜名查询</a></p>
     </center>
    </body>
    </html>

  • <!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>
      <form action="<%=basePath%>/SelectServlet" 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>


    <!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>


写回答

1回答

造梦大师

提问者

2019-03-28


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
 <%@ page import="com.imooc.food.*"%>
 <%
    String path = request.getContextPath(); //path是project的名字
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<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>
    <%
     for(Food food:FoodDaolmpl.getAllFood()){
    %>
     <tr>
      <td><%=food.getId() %></td>
      <td><%=food.getName() %></td>
      <td><%=food.getTaste() %></td>
      <td><img src="<%=basePath%>upload/<%=food.getFoodAdress().substring(food.getFoodAdress().lastIndexOf("\\")+1) %>" /></td>
      <td><%=food.getPrice() %></td>
      <td><%=food.getDescribe() %></td>
     </tr>
    <%
     }
    %>
   </tbody>
  </table>
 </center>
</body>
</html>

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
 <%@ page import="com.imooc.food.*"%> 
 <%
    String path = request.getContextPath(); //path是project的名字
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<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>
    
     <tr>
      <td>${food.id }</td>
      <td>${food.name }</td>
      <td>${food.taste }</td>
      <td><img src="<%=basePath%>upload/${food.getFoodAdress().substring(food.getFoodAdress().lastIndexOf("\\")+1)}" /></td>
      <td>${food.price }</td>
      <td>${food.describe }</td>
     </tr>
    
   </tbody>
  </table>
 </center>
</body>
</html>


<!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>
  <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>菜&nbsp;&nbsp;名</td>
     <td><input type="text" name="foodName"></td>
    </tr>
    <tr>
     <td>口&nbsp;&nbsp;味</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>价&nbsp;&nbsp;格</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>


0
hhrismorgen
h 你好同学,因为代码中的问题比较多,建议你将作业提交到作业区,在作业区,我们的老师会提供给同学一个详细的批复文档来供同学参考,这样修改的内容会更详细。方便同学理解并修改作业。祝学习愉快~
h019-03-28
共1条回复

0 学习 · 10204 问题

查看课程