菜品添加功能实现了,修改删除和查询有问题
1.老师我接收数据的方法写在一个java类里,这样有什么问题吗? 2.菜品修改功能点击修改之后,页面跳转到一个白页,是是因为共用这个接收数据的方法有问题吗?还是FoodDaolpml这个类里面的bd集合不是同一个,所以修改的时候里面没有所以出现白页?或者是其他什么原因? 3.点击删除和根据菜品名称查询,页面重定向都不成功,跳转到一个白页 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; 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.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); 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; 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); FoodDaolmpl.updateFood(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 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()+"/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);
request.getRequestDispatcher("/showFoodList.jsp").forward(request, response);
}else{
request.getRequestDispatcher("/showFoodList.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);
}
}
正在回答 回答被采纳积分+1
<%@ 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>
1、你这么写是没有问题的。
2、同学重定向之后是指showFoodList.jsp页面为空白页吗?建议同学使用转发试试,如果问题没有解决,建议你将showFoodList.jsp页面的代码也粘贴上来,只有产看了你具体的逻辑才能找出原因,祝学习愉快~
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星