封装进FoodDaoImpl的list中怎么取出全部信息
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 {
//创建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);
}
}
正在回答 回答被采纳积分+1
如下图所示,因为db是存储所有菜品的数据,你可以使用servlet调用FoodDaoImpl中的getAllFood方法就可以获取装有全部菜品信息的list集合,在getAllFood方法中直接return db 集合即可,祝学习愉快~
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 null;
}
//根据菜品名称查询菜品信息
public List<Map<String, Object>> getFoodByName(String foodName){
return null;
}
//菜品修改
public void updateFood(Map<String, Object> food){
}
//根据菜品ID进行删除
public void deleteFoodById(String id){
}
}
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星