老师,不知道怎么在查询菜品的时候只展示一条菜品,
每次吊机查询都是一起出来,而且删除都是无效的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | package servlet; import foodUtils.FoodDaoIml; 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 java.io.IOException; @WebServlet ( "/SelectServlet" ) public class SelectServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding( "UTF-8" ); //实例化调取命令查询对应食物集合 FoodDaoIml foodDaoIml = new FoodDaoIml(); //从表单获取查询名称 String foodName = request.getParameter( "foodName" ); //判断如果为空提示错误 if (request.getParameter( "foodName" )!= "" &&request.getParameter( "foodName" )==foodName) { request.getRequestDispatcher( "/showFoodList.jsp" ).forward(request, response); return ; } //判断如果输入的名字重复 if (foodDaoIml.getFoodByName(foodName).equals(foodName)) { request.setAttribute( "msg" , "不存在此foodName的菜品" ); request.getRequestDispatcher( "/selectFoodByName.jsp" ).forward(request, response); return ; } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | <%@ page import = "foodUtils.CookieUtils" %> <%@ page contentType= "text/html;charset=UTF-8" language= "java" %> <html> <head> <% String foodName = "" ; if (session.getAttribute( "foodName " )!= null ) {foodName =(String) session.getAttribute( "foodName " ); } %> <meta http-equiv= "Content-Type" content= "text/html; charset=utf-8" > <%@ taglib prefix= "c" uri= "http://java.sun.com/jsp/jstl/core" %> <title>菜品查询选项页面</title> <style type= "text/css" > </style> </head> <body> <center> <% request.setCharacterEncoding( "UTF-8" ); // 获得从客户端携带过来的所有的Cookie Cookie[] cookies = request.getCookies(); // 从Cookie的数组中查找指定名称的Cookie Cookie cookie = CookieUtils.findCookie(cookies, "foodName" ); if (cookie != null ){ foodName= cookie.getValue(); } if (session.getAttribute( "foodName" )!= null ){ foodName = (String)session.getAttribute( "foodName" ); } String msg = "" ; if (request.getAttribute( "msg" )!= null ){ msg = (String)request.getAttribute( "msg" ); } %> <h3><font color= "red" ><%=msg %></font></h3> <p><a href= "/servlet09/showFoodList.jsp" >查询所有菜品信息</a></p> <p><a href= "/servlet09/selectFoodByName.jsp" >菜名查询</a></p> </center> </body> </html> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | package servlet; import foodUtils.FoodDaoIml; 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 java.io.IOException; @WebServlet ( "/SelectServlet" ) public class SelectServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding( "UTF-8" ); //实例化调取命令查询对应食物集合 FoodDaoIml foodDaoIml = new FoodDaoIml(); //从表单获取查询名称 String foodName = request.getParameter( "foodName" ); //判断如果为空提示错误 if (request.getParameter( "foodName" )!= "" &&request.getParameter( "foodName" )==foodName) { request.getRequestDispatcher( "/showFoodList.jsp" ).forward(request, response); return ; } //判断如果输入的名字重复 if (foodDaoIml.getFoodByName(foodName).equals(foodName)) { request.setAttribute( "msg" , "不存在此foodName的菜品" ); request.getRequestDispatcher( "/selectFoodByName.jsp" ).forward(request, response); return ; } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | <%@ page import = "foodUtils.CookieUtils" %> <%@ page import = "foods.Food" %> <%@ page import = "java.util.List" %> <%@ page language= "java" contentType= "text/html; charset=UTF-8" pageEncoding= "UTF-8" %> <%@ taglib prefix= "c" uri= "http://java.sun.com/jsp/jstl/core" %> <html> <head> <meta http-equiv= "Content-Type" content= "text/html; charset=utf-8" > <title>菜品信息展示</title> <style type= "text/css" > <% String id = "" ; if (session.getAttribute( "id" )!= null ) {id =(String) session.getAttribute( "id" ); } %> </style> </head> <body> <center> <h1>菜品查询</h1> <% request.setCharacterEncoding( "UTF-8" ); String foodName= "" ; // 获得从客户端携带过来的所有的Cookie Cookie[] cookies = request.getCookies(); // 从Cookie的数组中查找指定名称的Cookie Cookie cookie = CookieUtils.findCookie(cookies, "foodName" ); if (cookie != null ){ foodName= cookie.getValue(); } if (session.getAttribute( "foodName" )!= null ){ foodName = (String)session.getAttribute( "foodName" ); } String msg = "" ; if (request.getAttribute( "msg" )!= null ){ msg = (String)request.getAttribute( "msg" ); } %> <h3><font color= "red" ><%=msg %></font></h3> <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> <% String basePath=request.getScheme()+ "://" +request.getServerName()+ ":" +request.getServerPort()+request.getContextPath()+ "/" ; %> <% List<Food> foodList = (List<Food>) request.getServletContext().getAttribute( "list" ); Food food = (Food)request.getAttribute( "food" ); if (food!= null ){%> <tr> <td><%=food.getId()%></td> <td><%=food.getFoodName()%></td> <td><%=food.getFoodTaste()%></td> <td align= "center" ><img src= "<%=basePath%>/upload/<%=food.getPath()%>" /></td> <td><%=food.getPrice()%></td> <td><%=food.getDescription()%></td> </tr> <% } else { for (Food food1 : foodList) {%> <tr> <td><%=food1.getId()%></td> <td><%=food1.getFoodName()%></td> <td><%=food1.getFoodTaste()%></td> <td align= "center" ><img src= "<%=basePath%>/upload/<%=food1.getPath()%>" /></td> <td><%=food1.getPrice()%></td> <td><%=food1.getDescription()%></td> </tr> <%} } %> </tbody> </table> </center> </body> </html> |
正在回答 回答被采纳积分+1
同学,你好!先确认一下,你显示所有菜品的功能是没问题的吗?咱们先来解决查询菜品只展示一条菜品的问题,然后再解决修改和删除的问题。
先来看一下,如果只展示一条菜品,应该是根据菜品名称进行查询,这时候就需要在表单中输入菜品名称,你用的是下面这个jsp。在这个jsp中会将foodName这个文本框的值提交给SelectByName这个Servlet进行处理。这个没有问题。但是此处还有一个隐藏域,这个不知道做什么的?
下面看一下SelectByName这个Servlet,在这个Servlet中要做一个查询操作,然后将查询结果存放到request中,再转发到jsp页面进行展示。
在下面的Servlet中,我们并没有看到查询,你是做了一个判断,但是只是比较和food对象中的name值是否相等。而food是新创建的对象,它的name属性并没有赋值,所以比较结果肯定是不相等的。应该如何比较呢?你可以在FoodDaoIml类中看一下是否有通过名称查询Food对象的方法,我看前面你提到了getFoodByName()方法,这个方法的返回值要是Food。
如果有getFoodByName方法,那么就调用该方法得到Food类的对象food。然后使用req.setAttribute("food",food);将数据存储到request中,然后转发到jsp页面进行访问,建议再写一个jsp页面,直接取出food的属性显示就可以了。不要使用之前显示所有菜品的jsp。
建议同学,先把按菜品查询功能做处理,做的过程中还有问题,可以继续提问。这个功能完成后,再去实现其他功能。如果问题太多,也可以提交一次作业,老师会对代码进行联调,会给出修改建议。
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
selectByName.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | <%@ page import = "foodUtils.CookieUtils" %> <%@ page contentType= "text/html;charset=UTF-8" language= "java" %> <html> <head> <meta http-equiv= "Content-Type" content= "text/html; charset=utf-8" > <title>菜名查询</title> <style type= "text/css" > </style> </head> <body> <%String foodName = "" ; request.setCharacterEncoding( "UTF-8" ); // 获得从客户端携带过来的所有的Cookie Cookie[] cookies = request.getCookies(); // 从Cookie的数组中查找指定名称的Cookie Cookie cookie = CookieUtils.findCookie(cookies, "foodName" ); if (cookie != null ){ foodName= cookie.getValue(); } if (session.getAttribute( "foodName" )!= null ){ foodName = (String)session.getAttribute( "foodName" ); } String msg = "" ; if (request.getAttribute( "msg" )!= null ){ msg = (String)request.getAttribute( "msg" ); } %> <h3><font color= "red" ><%=msg %></font></h3> <center> <h1>菜名查询</h1> <form action= "/servlet09/SelectByName" method= "post" > <input type= "hidden" name= "type" value= "2" > <table width= "400px" border= "1px" cellspacing= "0px" cellpadding= "0px" > <tr> <td>菜名</td> <td><input type= "text" name= "foodName" ></td> </tr> <tr> <td colspan= "2" style= "text-align:center" ><input type= "submit" value= "查询" ></td> </tr> </table> </form> </center> </body> </html> |
SelectByName(servlet)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | package servlet; import foodUtils.FoodDaoIml; import foods.Food; 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 java.io.IOException; import java.util.List; @WebServlet ( "/SelectByName" ) public class SelectByName extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding( "UTF-8" ); String foodName = req.getParameter( "foodName" ); // 从ServletContext域中获得保存用户信息集合: Food food = new Food(); if (foodName.equals(food.getFoodName())){ req.setAttribute( "msg" , "查询成功!" ); resp.sendRedirect( "/showFoodList.jsp" ); } else { req.setAttribute( "msg" , "菜名错误或者此菜不存在!返回全部菜单" ); req.getRequestDispatcher( "/showFoodList.jsp" ).forward(req, resp); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req,resp); } } |
- 参与学习 716 人
- 提交作业 185 份
- 解答问题 1363 个
会Java?懂前端基础?想学后台开发?那么,赶快来学习《Java Web入门》路径吧。本路径主要介绍Java Web的基础知识,并配有大量案例,定会让你收获多多!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧