跳转添加菜品报错
HTTP Status 500 - com.iomooc.domain.Food cannot be cast to java.util.Map
type Exception report
message com.iomooc.domain.Food cannot be cast to java.util.Map
description The server encountered an internal error that prevented it from fulfilling this request.
exception
java.lang.ClassCastException: com.iomooc.domain.Food cannot be cast to java.util.Map
com.imooc.select.FoodAddServlet.doGet(FoodAddServlet.java:99)
com.imooc.select.FoodAddServlet.doPost(FoodAddServlet.java:111)
javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/8.0.47 logs.
Apache Tomcat/8.0.47
正在回答
Servlet中的获得存放菜品信息对象的这条语句,foodDaolmpl.addFood((Map<String, Object>) food);你把一个food对象强制转换成了Map<String,Object>对象了,你看一下addFood()方法的参数具体是什么,如果就是Map<String,Object>,那么就不用把map中的信息都取出来生成一个Food的对象了,直接用foodDaolmpl.addFood(map)就可以了。因为map本身存放的就是菜品的信息了。这道题里,map中存的就是菜品信息,不用转存到Food里就行。
<%
FoodDaolmpl foodDaolmpl = new FoodDaolmpl();
Food food = new Food();
List<Map<String, Object>> db =foodDaolmpl.getAllFood();
if (!db.isEmpty()) {%>
<%for (Map<String, Object> map : db) {%>
<tr>
<td>
<%=map.get("id")%>
</td>
<td>
<%= map.get("foodName")%>
</td>
<td>
<%=map.get("taste")%>
</td>
<td>
<%= map.get("foodImage")%>
</td>
<td>
<%=map.get("price")%>
</td>
<td>
<%=map.get("description")%>
</td>
</tr>
<%}%>
<%}%>
能够添加除了图片的之外的值了,但是图片为空值
food=(Food)map.get(key);jsp中的这句话有问题,map中取出来的不是food,map中存的是id、price这些信息,也就是你展示出来的信息。所以既然map里存放的就是你想要的,就不用food了,直接通过map.get()就可以了。所以,下面这个循环的内容去掉。
for(String key:map.keySet()){
food=(Food)map.get(key);
}
把<tr></tr>标签所有内容都放在循环里, <%food.getId(); %>改为map.get(key),其他几项也这么改。key值就是你存放到map时的值,在Servlet里能查到,Food food = new Food(map.get("id"), map.get("foodName"), map.get("taste"), map.get("foodImage"), map.get("price"), map.get("description"));就是这句话里的那么值,id,foodName这些。
注意这部分Food类没用上,用的是map代替了Food类,所以就不用Food类了。
祝学习愉快!
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="com.imooc.select.*,com.imooc.utils.*,com.iomooc.domain.*,java.util.*"%>
<!DOCTYPE html>
<html>
<head>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<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" enctype="multipart/form-data">
<thead>
<tr>
<th>菜品ID</th>
<th>菜名</th>
<th>口味</th>
<th>菜品图片</th>
<th>价格</th>
<th>菜品描述</th>
</tr>
</thead>
<tbody>
<%
FoodDaolmpl foodDaolmpl = new FoodDaolmpl();
Food food = new Food();
List<Map<String, Object>> db =foodDaolmpl.getAllFood();
if (!db.isEmpty()) {
for (Map<String, Object> map : db) {
for(String key:map.keySet()){
food=(Food)map.get(key);
}
}
}
%>
<tr>
<td>
<%food.getId(); %>
</td>
<td>
<%food.getDishes(); %>
</td>
<td>
<%food.getTaste(); %>
</td>
<td>
<%food.getPath(); %>
</td>
<td>
<%food.getPrice(); %>
</td>
<td>
<%food.getDescribe(); %>
</td>
</tr>
</tbody>
</table>
</center>
</body>
</html>
/**
* 添加菜品信息的select
*/
@WebServlet("/FoodAddServlet")
public class FoodAddServlet extends HttpServlet {
@SuppressWarnings("unchecked")
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集合,List集合中存放的是FileItem对象
List<FileItem> list = servletFileUpload.parseRequest(request);
// 定义一个List集合,用于保存菜品口味数据:
List<String> tasteList = new ArrayList<String>();
// 4.遍历集合,获得每个FileItem,判断是表单项还是文件上传项
String url = null;
for (FileItem fileItem : list) {
// 判断是表单项还是文件上传项
if (fileItem.isFormField()) {
// 普通表单项:
// 接收表单项参数的值:
String name = fileItem.getFieldName(); // 获得表单项的name属性的值
String value = fileItem.getString("UTF-8");// 获得表单项的值
// 接收复选框的数据
if ("taste".equals(name)) {
String tasteValue = fileItem.getString("UTF-8");
// 将爱好的数据存入到Map集合中:
// 接收到一个值,将这个值存入到tasteList集合中
tasteList.add(tasteValue);
tasteValue = tasteList.toString().substring(1, tasteList.toString().length()-1); // xxx,yyy
System.out.println(name+" "+tasteList);
// 将菜品口味的数据存入到Map集合中:
map.put(name, tasteValue);
} else {
// 将数据存入到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();
}
}
}
// 封装数据到Food当中:
Food food = new Food(map.get("id"), map.get("foodName"), map.get("taste"), map.get("foodImage"), map.get("price"), map.get("description"));
// 获得存放菜品信息对象:
FoodDaolmpl foodDaolmpl = new FoodDaolmpl();
foodDaolmpl.addFood((Map<String, Object>) food);
//跳转到菜品信息展示页面showFoodList.jsp
response.sendRedirect(request.getContextPath()+"/showFoodList.jsp");
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星