修改菜品信息,不能进入到doPost方法中!
FoodUpdateServlet.java
package com.imooc.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.model.Food;
import com.imooc.model.FoodDaoImpl;
import com.imooc.utils.UploadUtils;
/**
* Servlet implementation class FoodUpdateServlet
*/
@WebServlet("/updateservlet")
public class FoodUpdateServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static FoodDaoImpl foodDaoImpl = new FoodDaoImpl();
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("----");
try {
DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
ServletFileUpload servletFileUpload = new ServletFileUpload(diskFileItemFactory);
List<FileItem> list = servletFileUpload.parseRequest(request);
System.out.println(list);
List<String> tastelist = new ArrayList<String>();
Map<String, String> map = new HashMap<String, String>();
for (FileItem fileItem : list) {
if (fileItem.isFormField()) {
String name = fileItem.getFieldName();
String value = fileItem.getString("UTF-8");
System.out.println(name + " " + value);
if (name.equals("taste")) {
String taste = fileItem.getString("UTF-8");
tastelist.add(taste);
} else {
map.put(name, value);
}
} else {
String fileName = fileItem.getName();
if (fileName != null && !fileName.equals("")) {
String uuidFileName = UploadUtils.getUUIDFileName(fileName);
InputStream is = fileItem.getInputStream();
String path = this.getServletContext().getRealPath("/upload");
String 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();
map.put("foodImage", uuidFileName);
}
}
}
String taste = tastelist.toString().replace("[", "").replace("]", "");
map.put("taste", taste);
foodDaoImpl.updateFood(creatFood(map));
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
public Food creatFood(Map<String, String> map) {
String id = map.get("id");
String foodName = map.get("foodName");
String taste = map.get("taste");
String foodImage = map.get("foodImage");
double price = Double.parseDouble(map.get("price"));
String description = map.get("description");
Food food = new Food(id, foodName, taste, foodImage, price, description);
return food;
}
}updateFood.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="/Vegetable_manage/updateservlet" 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>d <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>
0
收起
正在回答 回答被采纳积分+1
1回答
相似问题
登录后可查看更多问答,登录/注册
从网页搭建入门Java Web2018版
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10204 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程



恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星