老师,我的查询好像用不了,是哪里出错了呢?
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>图书后台管理</title>
<link rel="stylesheet" href="css/index.css">
<link rel="stylesheet" href="css/bootstrap.min.css">
</head>
<body>
<header>
<div class="container">
<nav>
<a href="bookList.jsp" >图书信息管理</a>
</nav>
<nav>
<a href="categoryList.jsp" >分类管理</a>
</nav>
</div>
</header>
<section class="banner">
<div class="container">
<div>
<h1>图书管理系统</h1>
<p>图书信息管理</p>
</div>
</div>
</section>
<section class="main">
<div class="container">
<form class="form-horizontal" action="${pageContext.request.contextPath }/AddBookServlet" method="post">
<div class="form-group" style="float: right;">
<div class="col-sm-offset-2 col-sm-10">
<button type="button" class="btn btn-primary" id="search">查询</button>
</div>
</div>
<div class="form-group" style="float: right;width: 300px;">
<div class="col-sm-8">
<input name="searchContent" class="form-control" id="searchContent"
placeholder="输入要查询的分类" style="width: 250px">
</div>
</div>
</form>
</div>
<div class="container">
<table class="table table-striped">
<thead>
<tr>
<th>序号</th>
<th>图书编号</th>
<th>图书名称</th>
<th>分类</th>
<th>价格</th>
<th>图书封面</th>
<th>操作</th>
</tr>
</thead>
<tbody id="cont">
<c:forEach items="${bookList }" var="b" varStatus="bookStatus">
<tr id="tr1">
<td>${bookStatus.index +1 }</td>
<td>${b.bid }</td>
<td>${b.bname }</td>
<td>${b.bcategory }</td>
<td>¥${b.price }</td>
<td><img src="${b.fengmian }"></td>
<td>
<a href="${pageContext.request.contextPath }/SerchBookServlet?bookId=${b.bid}">修改</a>
<a href="${pageContext.request.contextPath }/DeleteBookServlet?bookId=${b.bid}">删除</a>
</td>
<!--在循环显示数据时,此处的book0001可以用EL表达式进行替换-->
</tr>
</c:forEach>
</tbody>
</table>
</div>
</section>
<section class="page">
<div class="container">
<div id="fatie">
<a href="addBook.jsp"><button>新建</button></a>
</div>
</div>
</section>
<footer>
copy@慕课网
</footer>
</body>
<script type="text/javascript" src="js/jquery-3.3.1.js" ></script>
<script type="text/javascript">
$("#search").click(function(){
$.ajax({
"url":"${pageContext.request.contextPath}/SerchBookServlet",
"type":"post",
"data":{'id':$("#searchContent").val()},
"dataType":"json",
"success":function(json){
var content="";
for(var i=0;i<json.length;i++){
content=content+ "<tr><td>"
+ (i + 1)
+ "</td><td>"
+ json[i].bid
+ "</td><td>"
+ json[i].bname
+ "</td><td>"
+ json[i].bcategory
+ "</td><td>"
+ json[i].price
+"<td><img src='"+json[i].fengmian+"'></td>"
+ "</td><td> <a href='${pageContext.request.contextPath}/SerchBookServlet?bookId="
+ json[i].bid
+ "'>修改</a><a href='${pageContext.request.contextPath}/DeleteBookServlet?bookId="
+ json[i].bid + "'>删除</a></td></tr>";
$("#cont>tr").remove();
}
$("#cont").html(content);
}
});
});
</script>
</html>
package com.imooc.web.servlet;
import java.io.IOException;
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 com.imooc.domain.Book;
import com.imooc.impl.BookServiceImpl;
import com.imooc.service.BookService;
@WebServlet("/SerchBookServlet")
public class SerchBookServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//接收数据
String bookid=request.getParameter("bookId");
//封装数据
BookService bookservice=new BookServiceImpl();
//处理数据
Book book=bookservice.getBooksById(bookid);
request.setAttribute("book", book);
request.getRequestDispatcher("/updateBook.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
正在回答
同学你好,同学的SerchBookServlet类是否最终转发到了jsp页面呢?
同学的bookList.jsp文件中点击查询按钮,会发送ajax请求,在success中,会使用for循环遍历返回的json数据。所以SerchBookServlet类返回的json数据应该是一个集合。SerchBookServlet类不需要转发到jsp页面。
同学可以检查一下代码。参考代码如下:
如果SerchBookServlet类转发到了一个jsp页面,应该检查一下要转发的jsp文件中是否有bId,如果有,应该将其改为bid。
祝学习愉快~
- 参与学习 人
- 提交作业 676 份
- 解答问题 9666 个
本阶段将从前端网页搭建入手,到Java Web基础,前后端结合助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星