关于图书查询这一块的问题
package com.imooc.servlet; import java.io.IOException; import java.util.ArrayList; import java.util.List; 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 com.alibaba.fastjson.JSON; import com.imooc.dao.Book; import com.imooc.service.LibServicelmpl; /** * Servlet implementation class FindBookServlet */ @WebServlet("/FindBookServlet") public class FindBookServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public FindBookServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String bookID = request.getParameter("bookID"); String bookName = request.getParameter("bookName"); String catgoryName = request.getParameter("catgoryName"); List<Book> list = new ArrayList<Book>(); LibServicelmpl lsl = new LibServicelmpl(); List<Book> books = lsl.getLibDaolmpl().getBooks(); for(Book book : books) { if(book.getBookId().equals(bookID)) { list.add(book); }else if(book.getBookName().equals(bookName)) { list.add(book); }else if(book.getCfName().equals(catgoryName)) { list.add(book); } } String json = JSON.toJSONString(list); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <% String basePath=request.getScheme()+"://"+request.getServerName() +":"+request.getServerPort()+request.getContextPath(); %> <!DOCTYPE html> <html> <head> <base href="<%=basePath%>"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>图书查询</title> <script type="text/javascript" src="js/jquery-3.4.1.js"></script> <script type="text/javascript"> $("#search").on(click,function(){ $(function(){ "url" : "/library_system/FindBookServlet", "type" : "get", "data" : {"bookID":$("#bookID").val() , "bookName":$("#bookName").val() , "catgoryName" : $("#catgoryName").val()}, "dataType" : "json", "success" : function(json){ for(var i = 0 ; i < json.length; i++){ $("#cont").append( "<tr>" + "<th>" + json[i].bookId + "</th>" + "<th>" + json[i].bookName + "</th>" + "<th>" + json[i].cfName + "</th>" + "<th>" + json[i].price + "</th>" + "<th>" + json[i].describe + "</th>" + "</tr>"); } }, "error" : function(xmlhttp,errorText){ if(xmlhttp.status == 405){ alert("无效的请求方式"); }else if(xmlhttp.status == 404){ alert("未找到URL资源") }else if(xmlhttp.status == 500){ alert("服务器内部错误") }else{ alert("产生异常"); } } }); }); </script> </head> <body> <center> <h1>图书查询</h1> <p> 图书ID:<input type="text" name="bookID" id="bookID"> 图书名:<input type="text" name="bookName" id="bookName"> 分类:<input type="text" name="catgoryName" id="catgoryName"> <input type="submit" value="查询" id="search"> </p> <hr> <table width="800px" cellspacing="0px" cellpadding="0px" border="1px"> <thead> <tr> <th>图书ID</th> <th>书名</th> <th>分类</th> <th>价格</th> <th>描述</th> </tr> </thead> <tbody id="cont"> </tbody> </table> </center> </body> </html>
老师,我在查询的时候没有显示出所需要的图书,我的思路是:新建了一个showFindBook.jsp一个页面用来展示所查询到的图书,然后FindBookServlet是对这个页面的一些操作。我不知道这个思路是否是正确的,老师帮忙看看哪里有逻辑错误!
正在回答
同学你好,1、list可以输出,json有输出吗?System.out.println(json);有内容输出吗?
2、如果没有输出,list有输出,json没输出,同学JSON.toJSONString是否有正确的导入JSON的jar包呐,比如,老师项目中的json jar包。
如果同学还是有问题,同学可以将问题具体描述到文档中,作业老师会根据描述,具体定位问题。
如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <% String basePath=request.getScheme()+"://"+request.getServerName() +":"+request.getServerPort()+request.getContextPath(); %> <!DOCTYPE html> <html> <head> <base href="<%=basePath%>"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>图书查询</title> </head> <body> <center> <h1>图书查询</h1> <p> 图书ID:<input type="text" name="bookID" id="bookID"> 图书名:<input type="text" name="bookName" id="bookName"> 分类:<input type="text" name="catgoryName" id="catgoryName"> <input type="button" value="查询" id="search"> </p> <hr> <table width="800px" cellspacing="0px" cellpadding="0px" border="1px"> <thead> <tr> <th>图书ID</th> <th>书名</th> <th>分类</th> <th>价格</th> <th>描述</th> </tr> </thead> <tbody id="cont"> <c:forEach items="${booksList }" var="c"> <tr> <td>${c.bookId}</td> <td>${c.bookName }</td> <td>${c.cfName }</td> <td>${c.price }</td> <td>${c.describe }</td> </tr> </c:forEach> </tbody> </table> </center> <script type="text/javascript" src="<%=basePath %>/js/jquery-3.3.1.js"></script> <script type="text/javascript"> $("#search").click(function(){ $.ajax({ "url" : "/library_system/FindBookServlet", "type" : "get", "data" : {"bookID":$("#bookID").val() , "bookName":$("#bookName").val() , "catgoryName" : $("#catgoryName").val()}, "dataType" : "json", "success" : function(json){ var content = ""; console.log(json); $("#cont>tr").remove(); for(var i = 0 ; i < json.length; i++){ content = content + "<tr>" + "<th>" + json[i].bookId + "</th>" + "<th>" + json[i].bookName + "</th>" + "<th>" + json[i].cfName + "</th>" + "<th>" + json[i].price + "</th>" + "<th>" + json[i].describe + "</th>" + "</tr>"; } $("#cont").html(content); }, "error" : function(xmlhttp,errorText){ if(xmlhttp.status == 405){ alert("无效的请求方式"); }else if(xmlhttp.status == 404){ alert("未找到URL资源") }else if(xmlhttp.status == 500){ alert("服务器内部错误") }else{ alert("产生异常"); } } }) }); </script> </body> </html>
package com.imooc.servlet; import java.io.IOException; import java.util.ArrayList; import java.util.List; 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 com.alibaba.fastjson.JSON; import com.imooc.dao.Book; import com.imooc.service.LibServicelmpl; /** * Servlet implementation class FindBookServlet */ @WebServlet("/FindBookServlet") public class FindBookServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public FindBookServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String bookID = request.getParameter("bookID"); String bookName = request.getParameter("bookName"); String catgoryName = request.getParameter("catgoryName"); // 集合用于接收符号条件的图书 List<Book> list = new ArrayList<Book>(); LibServicelmpl lsl = new LibServicelmpl(); // 根据条件查询书籍信息 List<Book> books = lsl.getLibDaolmpl().getBooksByCondition(bookID, bookName, catgoryName); String json = JSON.toJSONString(list); System.out.println(json); response.setContentType("text/html;charset=utf-8"); response.getWriter().println(json); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package com.imooc.dao; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class LibDaolmpl { private static final List<User> userDb = new ArrayList<User>(); //用户表 private static final List<Map<String,Object>> categorys = new ArrayList<Map<String,Object>>(); //图书分类 private static final List<Book> books = new ArrayList<Book>(); //图书表 public static List<User> getUserdb() { return userDb; } public static List<Book> getBooks() { return books; } // 用户注册 public void regist(User user) { // 判断用户是否存在,存在则存入到集合中 if(isUserExist(user.getUsername()) == 1) { userDb.add(user); } } // 用户登录 public int login(String username,String password) { return 0; } // 判断指定的用户名在存储用户信息的集合中是否存在 public int isUserExist(String username) { // 遍历集合,若存在该用户,则返回-1,否则返回1 for(User u : userDb) { if(u.getUsername().equals(username)) { return -1; } } return 1; } // 添加图书分类 public String addBookCatgory(Long id,String catgoryName,String description) { Map<String,Object> map = new HashMap<String,Object>(); map.put("id", id); map.put("catgoryName", catgoryName); map.put("description", description); for(Map m : categorys) { // 判断id是否存在 if(m.get("id").equals(id)) { // 判断分类名称是否一样 if(m.get("catgoryName").equals(catgoryName)) { return "添加失败"; } return "添加失败"; } } categorys.add(map); return "添加成功"; } // 获取所有图书分类 public List<Map<String,Object>> getAllBookCatgory(){ return categorys; } // 添加图书 public String addBook(Book book) { if(books != null) { for(Book b : books) { if(b.getBookId().equals(book.getBookId())) { return "添加图书失败"; } } books.add(book); return "添加图书成功"; }else { books.add(book); return "添加图书成功"; } } // 根据条件查询书籍信息 public List<Book> getBooksByCondition(String bookID,String bookName,String catgoryName){ // 新建一个集合用于存放符合条件的图书 List<Book> book = new ArrayList<Book>(); for(Book b : books) { if(b.getBookId().equals(bookID)) { // 图书ID有且只有一个 book.add(b); return book; }else if(b.getBookName().equals(bookName)) { // 图书名称可以有多个?这里设置为只能有一个 book.add(b); return book; }else if(b.getCfName().equals(catgoryName)) { // 图书分类名称相同的可有多个 book.add(b); continue; // 若找到图书分类名称,结束该循环开始下一次 } } return book; } }
老师,在你的代码中能显示出来吗?可是我的确没有查询到。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <% String basePath=request.getScheme()+"://"+request.getServerName() +":"+request.getServerPort()+request.getContextPath(); %> <!DOCTYPE html> <html> <head> <base href="<%=basePath%>"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>图书查询</title> </head> <body> <center> <h1>图书查询</h1> <p> 图书ID:<input type="text" name="bookID" id="bookID"> 图书名:<input type="text" name="bookName" id="bookName"> 分类:<input type="text" name="catgoryName" id="catgoryName"> <input type="button" value="查询" id="search"> </p> <hr> <table width="800px" cellspacing="0px" cellpadding="0px" border="1px"> <thead> <tr> <th>图书ID</th> <th>书名</th> <th>分类</th> <th>价格</th> <th>描述</th> </tr> </thead> <tbody id="cont"> <c:forEach items="${booksList }" var="c"> <tr> <td>${c.bookId}</td> <td>${c.bookName }</td> <td>${c.cfName }</td> <td>${c.price }</td> <td>${c.describe }</td> </tr> </c:forEach> </tbody> </table> </center> <script type="text/javascript" src="<%=basePath %>/js/jquery-3.3.1.js"></script> <script type="text/javascript"> $("#search").click(function(){ $.ajax({ "url" : "/library_system/FindBookServlet"; "type" : "get", "data" : {"bookID":$("#bookID").val() , "bookName":$("#bookName").val() , "catgoryName" : $("#catgoryName").val()}, "dataType" : "json", "success" : function(json){ var content = ""; console.log(json); $("#cont>tr").remove(); for(var i = 0 ; i < json.length; i++){ content = content + "<tr>" + "<th>" + json[i].bookId + "</th>" + "<th>" + json[i].bookName + "</th>" + "<th>" + json[i].cfName + "</th>" + "<th>" + json[i].price + "</th>" + "<th>" + json[i].describe + "</th>" + "</tr>"; } $("#cont").html(content); }, "error" : function(xmlhttp,errorText){ if(xmlhttp.status == 405){ alert("无效的请求方式"); }else if(xmlhttp.status == 404){ alert("未找到URL资源") }else if(xmlhttp.status == 500){ alert("服务器内部错误") }else{ alert("产生异常"); } } }) }); </script> </body> </html>
package com.imooc.servlet; import java.io.IOException; import java.util.ArrayList; import java.util.List; 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 com.alibaba.fastjson.JSON; import com.imooc.dao.Book; import com.imooc.service.LibServicelmpl; /** * Servlet implementation class FindBookServlet */ @WebServlet("/FindBookServlet") public class FindBookServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public FindBookServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String bookID = request.getParameter("bookID"); String bookName = request.getParameter("bookName"); String catgoryName = request.getParameter("catgoryName"); // 集合用于接收符号条件的图书 List<Book> list = new ArrayList<Book>(); LibServicelmpl lsl = new LibServicelmpl(); // 根据条件查询书籍信息 List<Book> books = lsl.getLibDaolmpl().getBooksByCondition(bookID, bookName, catgoryName); String json = JSON.toJSONString(list); System.out.println(json); response.setContentType("text/html;charset=utf-8"); response.getWriter().println(json); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
老师,我不贴图片了,图片一直传不上去,问题还是跟之前的一样。我换了个3.3.1还是一样。console中的错误:showBooks.jsp:49 Uncaught SyntaxError: Unexpected token ;
然后url那边还是有×
同学你好,Uncaught SyntaxError: Unexpected token :表示没有正确的获取到这个dom元素,同学可能是如下问题导致这个错误:
1、jquery文件的版本问题,同学可以更换另一个版本的文件,例如:
复制另一个jquery文件,重新引入
2、将单击事件放在页面底部,因为页面的加载是根据顺序来加载的,id为search的元素的在它加载完成后,再通过jq的id获取。
3、ajax的请求写法如下:
使用jq发送ajax不要使用$(function(){}.
如上修改后,同学如果还有问题,同学可以将自己修改后的代码贴出,便于老师根据同学的代码来调整错误。注意不要贴在回复中,会失去代码格式,可以将修改后的代码贴在"我要回答"中。
如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~
同学你好,检查运行贴出代码,发送ajax请求,还有如下错误内容:
1、jquery文件没有加载到,原因为在页面中添加了<base href="<%=basePath%>">,对应获取文件时,建议按照如下写法
如果不想按照这个写法,可以直接去掉base 的内容。
2、查询按钮建议改为button,submit是提交按钮,默认会刷新页面,修改建议如下:
3、添加单击事件与发送ajax的请求写法如下:
4、在添加之前,建议删掉#cont中tr的内容,而不是#cont中的内容,例如:
运行代码,找到如上问题,如果还有问题,建议同学将自己的具体报错信息描述一下。
如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <% String basePath=request.getScheme()+"://"+request.getServerName() +":"+request.getServerPort()+request.getContextPath(); %> <!DOCTYPE html> <html> <head> <base href="<%=basePath%>"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>图书查询</title> </head> <script type="text/javascript" src="js/jquery-3.4.1.js"></script> <script type="text/javascript"> $("#search").on(click,function(){ $(function(){ "url" : "/library_system/FindBookServlet", "type" : "get", "data" : {"bookID":$("#bookID").val() , "bookName":$("#bookName").val() , "catgoryName" : $("#catgoryName").val()}, "dataType" : "json", "success" : function(json){ console.log(json); $("#cont").remove(); for(var i = 0 ; i < json.length; i++){ $("#cont").append( "<tr>" + "<th>" + json[i].bookId + "</th>" + "<th>" + json[i].bookName + "</th>" + "<th>" + json[i].cfName + "</th>" + "<th>" + json[i].price + "</th>" + "<th>" + json[i].describe + "</th>" + "</tr>"); } }, "error" : function(xmlhttp,errorText){ if(xmlhttp.status == 405){ alert("无效的请求方式"); }else if(xmlhttp.status == 404){ alert("未找到URL资源") }else if(xmlhttp.status == 500){ alert("服务器内部错误") }else{ alert("产生异常"); } } }); }); </script> <body> <center> <h1>图书查询</h1> <p> 图书ID:<input type="text" name="bookID" id="bookID"> 图书名:<input type="text" name="bookName" id="bookName"> 分类:<input type="text" name="catgoryName" id="catgoryName"> <input type="submit" value="查询" id="search"> </p> <hr> <table width="800px" cellspacing="0px" cellpadding="0px" border="1px"> <thead> <tr> <th>图书ID</th> <th>书名</th> <th>分类</th> <th>价格</th> <th>描述</th> </tr> </thead> <tbody id="cont"> <c:forEach items="${booksList }" var="c"> <tr> <td>${c.bookId}</td> <td>${c.bookName }</td> <td>${c.cfName }</td> <td>${c.price }</td> <td>${c.describe }</td> </tr> </c:forEach> </tbody> </table> </center> </body> </html>
package com.imooc.servlet; import java.io.IOException; import java.util.ArrayList; import java.util.List; 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 com.alibaba.fastjson.JSON; import com.imooc.dao.Book; import com.imooc.service.LibServicelmpl; /** * Servlet implementation class FindBookServlet */ @WebServlet("/FindBookServlet") public class FindBookServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public FindBookServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String bookID = request.getParameter("bookID"); String bookName = request.getParameter("bookName"); String catgoryName = request.getParameter("catgoryName"); // 集合用于接收符号条件的图书 List<Book> list = new ArrayList<Book>(); LibServicelmpl lsl = new LibServicelmpl(); // 根据条件查询书籍信息 List<Book> books = lsl.getLibDaolmpl().getBooksByCondition(bookID, bookName, catgoryName); String json = JSON.toJSONString(list); System.out.println(json); response.setContentType("text/html;charset=utf-8"); response.getWriter().println(json); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package com.imooc.dao; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class LibDaolmpl { private static final List<User> userDb = new ArrayList<User>(); //用户表 private static final List<Map<String,Object>> categorys = new ArrayList<Map<String,Object>>(); //图书分类 private static final List<Book> books = new ArrayList<Book>(); //图书表 public static List<User> getUserdb() { return userDb; } public static List<Book> getBooks() { return books; } // 用户注册 public void regist(User user) { // 判断用户是否存在,存在则存入到集合中 if(isUserExist(user.getUsername()) == 1) { userDb.add(user); } } // 用户登录 public int login(String username,String password) { return 0; } // 判断指定的用户名在存储用户信息的集合中是否存在 public int isUserExist(String username) { // 遍历集合,若存在该用户,则返回-1,否则返回1 for(User u : userDb) { if(u.getUsername().equals(username)) { return -1; } } return 1; } // 添加图书分类 public String addBookCatgory(Long id,String catgoryName,String description) { Map<String,Object> map = new HashMap<String,Object>(); map.put("id", id); map.put("catgoryName", catgoryName); map.put("description", description); for(Map m : categorys) { // 判断id是否存在 if(m.get("id").equals(id)) { // 判断分类名称是否一样 if(m.get("catgoryName").equals(catgoryName)) { return "添加失败"; } return "添加失败"; } } categorys.add(map); return "添加成功"; } // 获取所有图书分类 public List<Map<String,Object>> getAllBookCatgory(){ return categorys; } // 添加图书 public String addBook(Book book) { if(books != null) { for(Book b : books) { if(b.getBookId().equals(book.getBookId())) { return "添加图书失败"; } } books.add(book); return "添加图书成功"; }else { books.add(book); return "添加图书成功"; } } // 根据条件查询书籍信息 public List<Book> getBooksByCondition(String bookID,String bookName,String catgoryName){ // 新建一个集合用于存放符合条件的图书 List<Book> book = new ArrayList<Book>(); for(Book b : books) { if(b.getBookId().equals(bookID)) { // 图书ID有且只有一个 book.add(b); return book; }else if(b.getBookName().equals(bookName)) { // 图书名称可以有多个?这里设置为只能有一个 book.add(b); return book; }else if(b.getCfName().equals(catgoryName)) { // 图书分类名称相同的可有多个 book.add(b); continue; // 若找到图书分类名称,结束该循环开始下一次 } } return book; } }
老师,我把代码改了一下,去掉了那个新建的页面。但是还是出现了错误。
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星