正在回答 回答被采纳积分+1
从同学的图片看,同学已经从集合中取出并展示出来了,现在唯一的就是菜名和菜品图片没取出来。
如果没有猜错,通常把菜品名称改成:
把
String foodname=(String)food.get("foodname");
改成:
String foodName=(String)food.get("foodName");
把
String foodimage=(String)food.get("foodimage");
改成:
String foodImage=(String)food.get("foodImage");
和下图添加的name一致,因为你的map中 存的key是与他们对应的。
、
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.util.*,com.imooc.java.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</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>
<%
FoodDaoImpl fdi=new FoodDaoImpl();
List<Map<String, Object>>list=fdi.getAllFood();
for(Map map:list){
Map<String, Object> food=map;
String id=(String)food.get("id");
String foodname=(String)food.get("foodname");
String taste=(String)food.get("taste");
String foodimage=(String)food.get("foodimage");
String price=(String)food.get("price");
String description=(String)food.get("description");
%>
<tr>
<td><%=id %></td>
<td><%=foodname %></td>
<td><%=taste %></td>
<td><%=foodimage %></td>
<td><%=price %></td>
<td><%=description %></td>
</tr>
<%
}
%>
</tbody>
</table>
</center>
</body>
</html>
package com.imooc.java;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class FoodDaoImpl {
private static final List<Map<String, Object>> db=new ArrayList<Map<String, Object>>();
//添加菜品
public void addFood(Map<String, Object> food){
//将传入的map集合添加进list集合中
db.add(food);
}
//查询所有菜品信息
public List<Map<String, Object>> getAllFood(){
//这里不知道怎么取出全部信息
return db;
}
//根据菜品名称查询菜品信息
public List<Map<String, Object>> getFoodByName(String foodName){
return null;
}
//菜品修改
public void updateFood(Map<String, Object> food){
}
//根据菜品ID进行删除
public void deleteFoodById(String id){
}
}
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.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.DiskFileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import com.imooc.java.Food;
import com.imooc.java.FoodDaoImpl;
import com.imooc.utils.UploadUtils;
/**
* Servlet implementation class RegistServlet
*/
@WebServlet("/RegistServlet")
public class RegistServlet extends HttpServlet {
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
try {
Food food=new Food();
//创建map集合Key值为食品ID value为食品对象
Map<String,Object> map=new HashMap<String,Object>();
//创建一个磁盘文件项工厂对象
DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
//创建核心解析类
ServletFileUpload servletFileUpload = new ServletFileUpload(diskFileItemFactory);
//解析request请求 返回List集合 List里是fileitem对象
List<FileItem> list = servletFileUpload.parseRequest(request);
//创建FoodDaoImpl对象
FoodDaoImpl fdi=new FoodDaoImpl();
String url=null;
//遍历取出里面的信息
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=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();
}
}
System.out.println(map);
//调用FoodDaoImpl对象方法把集合传到FoodDaoImpl类中
fdi.addFood(map);
//重定向跳转
response.sendRedirect(request.getContextPath()+"/le1.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 星