关于 JSONArray带回来的list的问题
老师我在查询图书中如果只是输入类别的话应该将所有该类别的书都显示出来对吧
但是在servlet中确认的一个books的list中装上所有同一个类别的所有书为什么用了
JSONArray jsonarray=new JSONArray(books);
response.getOutputStream().write(jsonarray.toString().getBytes("utf-8"));
之后带回来的数据中只会显示一本书的内容呢?
附上servlet代码
package com.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 org.json.JSONArray; import org.json.JSONObject; import com.book.Book; import com.lib.Lib; /** * Servlet implementation class showbookservelet */ @WebServlet("/showbookservelet") public class showbookservelet extends HttpServlet { private static final long serialVersionUID = 1L; public showbookservelet() { super(); // TODO Auto-generated constructor stub } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Lib lib=new Lib(); String bookid=request.getParameter("bookID"); String bookname=request.getParameter("bookName"); String catgoryName=request.getParameter("catgoryName"); List<Book>books =lib.getBooksByCondition(bookid, bookname, catgoryName); System.out.println("开始对servlet中的books进行验证"); for(Book book:books){ System.out.println(book.getBookname()); } JSONArray jsonarray=new JSONArray(books); response.getOutputStream().write(jsonarray.toString().getBytes("utf-8")); } /** * @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); } }
已经确认books中书的数量没有问题了
然后是jsp中的代码
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" import="com.lib.*,com.book.*" %> <!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>图书查询</title> <script type="text/javascript" src="/web_homework2/resources/js/jquery-1.4.2.js"></script> </head> <body> <center> <h1>图书查询</h1> <p> 图书ID:<input type="text" name="bookID"> 图书名:<input type="text" name="bookName"> 分类:<input type="text" name="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"> <%Lib lib=new Lib(); %> <!-- 用ajax做动态新 --> <tr> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> </tbody> </table> </center> </body> <script type="text/javascript" > $("#search").click(function(){ //var search=document.getElementById("search"); $.ajax({ url:"/web_homework2/showbookservelet", type:"post", data:{ bookID:$("input[name=bookID]").val(), bookName:$("input[name=bookName]").val(), catgoryName:$("input[name=catgoryName]").val(), }, dataType:"json", // success:function(result){//result里面就封装了servlet给ajax传回来数据的结果 var list=eval(result); var content=null; if(list!=null){ for(var i in list){ var id=list[i].id; <% System.out.println("===========");%> var bookName=list[i].bookname; var catgoryName=list[i].bookclass; var price =list[i].bookprice; console.log(price); var description=list[i].bookdedescription; console.log(description); content=content+"</tr><td>"+id+"</td><td>"+ bookName+"</td><td>"+catgoryName+"</td><td>"+ price+"</td><td>"+description+"</td></tr>"; $("#cont>tr").remove(); } $("#cont").html(content); } } } ) }) </script> </html>
明明后端带来的books中有两本书啊
为什么传到前面后就只有一本了呢?
0
收起
正在回答 回答被采纳积分+1
2回答
我叫飞天德
2018-12-13 17:26:21
从新发一下
for(var i in list){ var id=list[i].id; <% System.out.println("===========");%> var bookName=list[i].bookname; var catgoryName=list[i].bookclass; var price =list[i].bookprice; console.log(price); var description=list[i].bookdedescription; console.log(description); content=content+"</tr><td>"+id+"</td><td>"+ bookName+"</td><td>"+catgoryName+"</td><td>"+ price+"</td><td>"+description+"</td></tr>"; $("#cont>tr").remove(); }
for里面只有这些吗
从网页搭建入门Java Web2018版
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星