我显示页面使用的jstl,怎么根据名字查询
package com.imooc.model; /** * 菜品类 * @author 888 * */ public class Food { private String fid;//菜品ID private String fname;//菜品名称 private String lavor;//口味 private String imgaddress;//菜品地址 private double peice;//价格 private String describe;//菜品描述 public Food() { } public Food(String fid, String fname, String lavor, String imgaddress, double peice, String describe) { super(); this.fid = fid; this.fname = fname; this.lavor = lavor; this.imgaddress = imgaddress; this.peice = peice; this.describe = describe; } public String getFid() { return fid; } public void setFid(String fid) { this.fid = fid; } public String getFname() { return fname; } public void setFname(String fname) { this.fname = fname; } public String getLavor() { return lavor; } public void setLavor(String lavor) { this.lavor = lavor; } public String getImgaddress() { return imgaddress; } public void setImgaddress(String imgaddress) { this.imgaddress = imgaddress; } public Double getPeice() { return peice; } public void setPeice(Double peice) { this.peice = peice; } public String getDescribe() { return describe; } public void setDescribe(String describe) { this.describe = describe; } @Override public String toString() { return "Food [fid=" + fid + ", fname=" + fname + ", lavor=" + lavor + ", imgaddress=" + imgaddress + ", peice=" + peice + ", describe=" + describe + "]"; } }
package com.imooc.model; import java.util.ArrayList; import java.util.List; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; public class FoodDaoImpl { private static final List<Food> list=new ArrayList<Food>(); //添加菜品 public void addFood(Food food) { list.add(food); } //查询所有菜品信息 public List<Food> getAllFood(){ return list; } //根据菜品名称查询菜品信息 public Food getFoodByName(String foodName) { for (Food f : list) { if (f.getFname().equals(foodName)) { return f; } } return null; } //根据菜品id查询菜品信息 public Food getFoodById(String id) { return null; } //菜品修改 public void updateFood(Food newFood) { } //根据菜品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.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.ServletContext; 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 AddFoodServlet */ @WebServlet("/AddFoodServlet") public class AddFoodServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { //1.创建磁盘文件项工厂对象 DiskFileItemFactory factory=new DiskFileItemFactory(); //2.创建核心解析类 ServletFileUpload fileUpload=new ServletFileUpload(factory); //3.解析request请求,返回list集合,list存放的是FileItem对象 List<FileItem> list =fileUpload.parseRequest(request); //定义一个list集合,用于保存复选框(品味)的数据 List<String> tastelList=new ArrayList<String>(); //定义一个Map集合,进行接收数据 Map<String, String> map=new HashMap<String, String>(); String url=""; for (FileItem fileItem : list) { if(fileItem.isFormField()) { //普通项 //获取name属性的值 String name=fileItem.getFieldName(); //获取普通表单项value的值,并解决中文乱码问题 String value=fileItem.getString("UTF-8"); if(name.equals("taste")) { String tasteValue=fileItem.getString("UTF-8"); //每接收到一个值,存到tastelList中 tastelList.add(tasteValue); tasteValue=tastelList.toString().substring(1,tastelList.toString().length()-1); map.put(name, tasteValue); }else { map.put(name, value); } }else { //文件上传项 //获得文件上传的名称 String fileName=fileItem.getName(); if(fileName!=null&&!fileItem.equals("")) { //通过工具类获得唯一文件名(十六位数字.jpg) String UUIDFileName=UploadUtils.getUUIDFileName(fileName); //获得文件上传的输入流 InputStream is= fileItem.getInputStream(); //获得文件上传的路径(获取到项目中文件upload的路径) String path= this.getServletContext().getRealPath("/upload"); //将输入流对接到输出流就可以了(将图片放入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); //获取ServletContext对象 ServletContext context= this.getServletContext(); List<Food> foodList = (List<Food>) context.getAttribute("list"); //校验用户名 if(map.get("foodName")!=null&&map.get("foodName").equals("")){ for (Food food : foodList) { if(food.getFname().equals(map.get("foodName"))) { request.setAttribute("msg", "菜名已存在!"); request.getRequestDispatcher("/addFood.jsp").forward(request, response); return; } } } double price=Double.parseDouble(map.get("price")); //获取图片绝对路径的最后一个"/" int idx=url.lastIndexOf("/"); //获取文件上传的唯一文件名(16位数字.xxx) String fileName=url.substring(idx+1); Food f=new Food(map.get("id"),map.get("foodName"), map.get("taste"),fileName, price,map.get("description")); System.out.println(f); //调用增加方法 FoodDaoImpl fdi=new FoodDaoImpl(); fdi.addFood(f); context.setAttribute("list", fdi.getAllFood()); response.sendRedirect("/variety_of_dishes/showFoodList.jsp"); } catch (FileUploadException e) { // TODO Auto-generated catch block e.printStackTrace(); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package com.imooc.utils; import java.util.UUID; /** * 文件上传工具类 * @author 888 * */ public class UploadUtils { /** * 生成唯一的文件名 * */ public static String getUUIDFileName(String fileName) { /*UUID:UUID是生成唯一的文件名,不会重复的。 唯一缺陷在于生成的结果串会比较长*/ //将xx.xxx截取成.xxx(如xx.jpg) int idx =fileName.lastIndexOf(".");//得到点的位置 String extention= fileName.substring(idx);//从点开始截取 //把"-"去掉 //UUID.randomUUID()返回一个十六位的数字组成 String uuidFileName=UUID.randomUUID().toString().replace("-", "")+extention; return uuidFileName; } public static void main(String[] args) { System.out.println(UUID.randomUUID()); } }
<
%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE html> <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> <c:forEach items="${applicationScope.list }" var="food"> <tr> <td>${food.fid }</td> <td>${food.fname }</td> <td>${food.lavor }</td> <td><img src="/variety_of_dishes/upload/${food.imgaddress }"></td> <td>${food.peice }</td> <td>${food.describe }</td> </tr> </c:forEach> </tbody> </table> </center> </body> </html>
0
收起
正在回答 回答被采纳积分+1
3回答
从网页搭建入门Java Web2018版
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星