查询疑问??
查询图书,遍历 ,int i=0,做下标记,就报错, ava.lang.IndexOutOfBoundsException: Index: 2, Size: 2,数组下标越界,但我想不通怎么越界了,但后我 改成 int i=-1;就没报错,正常了,但查询出来的数据有问题,查询出来的数据都是最后一个,请问下,这里我怎么改,才能正常查询数据了,谢谢。
//根据图书ID来查询书籍信息:
public Book getBooksByConditionId(String bookID) {
int i=0;
for(Book book:books) {
if(book.getBookId().equals(bookID)) {
break;
}
i++;
}
Book b=books.get(i);
return b;
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
String basePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath();
%>
<!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 }/QueryBookServlet" method="post">
<div class="form-group" style="float: right;">
<div class="col-sm-offset-2 col-sm-10">
<button id="query" type="button" class="btn btn-primary">查询</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="c" varStatus="idx">
<tr id="tr1">
<td>${idx.index+1}</td>
<td>${c.bookId }</td>
<td>${c.bookName }</td>
<td>${c.categoryName }</td>
<td>¥${c.bookPrice }</td>
<td><img src="${c.bookPic }"></td>
<td>
<a href="${pageContext.request.contextPath }/updateBook.jsp?bookId=${c.bookId }">修改</a>
<a href="${pageContext.request.contextPath }/DeleteBookServlet?bookId=${c.bookId }">删除</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>
<script type="text/javascript" src="js/jquery-3.3.1.js" ></script>
<script type="text/javascript" >
$("#query").click(function(){
$.ajax({
"url":"<%=basePath%>/QueryBookServlet",
"type":"get",
"dataType":"json",
"success":function(json){
var query1="";
for(var i=0;i<json.length;i++){
query1=query+"<tr><td>"+(i+1)
+"</td><td>"+json[i].bookId
+"</td><td>"+json[i].bookName
+"</td><td>"+json[i].categoryName
+"</td><td>¥"+json[i].bookPrice
+"</td><td><img src='"+json[i].bookPic+"'>"
+"</td><td><a href='${pageContext.request.contextPath }/updateBook.jsp?bookId="+json[i].bookId+"'>修改</a><a href='${pageContext.request.contextPath }/DeleteBookServlet?bookId="+json[i].bookId+"'> 删除</a>"
+"</td></tr>";
$("cont>tr").remove();
}
$("#cont").html(query1);
}
})
})
</script>
</body>
</html>
package com.imooc.web.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.domain.Book;
import com.imooc.service.impl.BookServiceImpl;
/**
* Servlet implementation class QueryBookServlet
*/
@WebServlet("/QueryBookServlet")
public class QueryBookServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public QueryBookServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String searchContent=request.getParameter("searchContent"); //用户输入要查询的数据
BookServiceImpl bookservice=new BookServiceImpl();
List<Book> booklist=bookservice.getbook(); //获取book集合
System.out.println("查询前集合为:"+booklist);
Book blist=bookservice.getBooksByConditionId(searchContent); //查询完毕了返回一个BOOK
List<Book> listbook=new ArrayList<Book>();
listbook.add(blist);
String json=JSON.toJSONString(listbook);
response.setContentType("text/html;charset=UTF-8");
response.getWriter().print(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.service.impl;
import java.util.ArrayList;
import java.util.List;
import com.imooc.domain.Book;
public class BookServiceImpl {
private static final List<Book> books=new ArrayList<Book>();
//添加图书
public void addBook(Book book) {
books.add(book);
}
//获取现在的图书
public List<Book> getbook() {
return books;
}
//图书删除
public void deleteBook(Book book) {}
//修改图书
public void updateBook(Book updateBook) {
for(Book book:books) {
if(book.getBookId().equals(updateBook.getBookId())) {
book.setBookName(updateBook.getBookName());
book.setBookPic(updateBook.getBookPic());
book.setBookPrice(updateBook.getBookPrice());
book.setCategoryName(updateBook.getCategoryName());
book.setRemarks(updateBook.getRemarks());
}
}
}
//根据图书ID来查询书籍信息:
public Book getBooksByConditionId(String bookID) {
int i=0;
for(Book book:books) {
if(book.getBookId().equals(bookID)) {
break;
}
i++;
}
Book b=books.get(i);
return b;
}
//根据分类来查询书籍信息:
public List<Book> getBooksByConditionName(String catgoryName) {
return null;
}
}
正在回答
同学你好!
很抱歉老师第一次忽略了同学页面的问题,没有考虑周全,还望同学见谅~
1.查看同学的代码,发现同学在ajax中并没有给后台传值呢
2.总是查询最后一个并不是同学的后台的问题,是由于同学没有传参,循环执行的次数是List集合大小,导致i会比正常的大,但同学将i的值改为-1后,没有异常了,但是找不到时由于i的值是最后一个,所以就会返回最后一个值
3.建议同学做如下修改:
后台可以改为如果没有查到数据,返回null,查询到就直接返回book
在页面上将要传递的参数加上
如果我的回答解决了你的疑惑,请采纳,祝学习愉快~
相似问题
登录后可查看更多问答,登录/注册
- 参与学习 人
- 提交作业 676 份
- 解答问题 9666 个
本阶段将从前端网页搭建入手,到Java Web基础,前后端结合助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星