老师帮忙看看那边错了,找了好久
<%@page contentType="text/html;charset=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">
<script type="text/javascript" src="js/jquery-3.4.1.min.js"></script>
<script type="text/javascript">
$(function(){
$("#submit").click(function (){
//接收输入框的值
var da=document.getElementById("searchContent").value;
var pat="${pageContext.request.contextPath}/searchBook";
$.ajax({
"url":pat,
"type":"post",
"data":{"t":da},
"dataType":"json",
"success":function(json){
for(var i=0;i<json.length;i++){
$("#tbody").html("<td>"+(i+1)+"</td>");
$("#tbody").html("<td>"+json[i].bookId+"</td>");
$("#tbody").html("<td>"+json[i].bookName+"</td>");
$("#tbody").html("<td>"+json[i].categoryName+"</td>");
$("#tbody").html("<td>"+json[i].price+"</td>");
$("#tbody").html("<td><imgsrc=json[i].path></td>");
var sc="<td><a href=${pageContext.request.contextPath }/update?bookId=json[i].bookId>修改</a><a href=${pageContext.request.contextPath }/deleteBook?bookId=json[i].bookId>删除</a></td>";
$("#tbody").html(sc);
}
}
})
})})
</script>
</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" method="post">
<div class="form-group" style="float: right;">
<div class="col-sm-offset-2 col-sm-10">
<button type="button" id="submit" 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="tbody">
<tr id="tr1">
<td>1</td>
<td>book0001</td>
<td>Java基础</td>
<td>计算机类</td>
<td>¥29</td>
<td><img src="img/g1.jpg"></td>
<td>
<a href="/updateBook?bookId=book0001">修改</a>
<a href="/deleteBook?bookId=book0001">删除</a>
</td>
<c:forEach items="${books }" var="b" varStatus="idx">
<tr id="tr1">
<td>${idx.index+2 }</td>
<td>${b.bookId }</td>
<td>${b.bookName }</td>
<td>${b.categoryName }</td>
<td>${b.price }</td>
<td><img src="${b.path }"></td>
<td>
<a href="${pageContext.request.contextPath }/update?bookId=${b.bookId }">修改</a>
<a href="${pageContext.request.contextPath }/deleteBook?bookId=${b.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>
</body>
</html>
package com.book.servlet;
import java.io.IOException;
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.book.domain.Book;
import com.book.service.BookService;
import com.book.service.impl.BookServiceImpl;
/**
* Servlet implementation class SearchServlet
*/
@WebServlet("/searchBook")
public class SearchServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public SearchServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//接收数据
String categoryName=request.getParameter("t");
//处理数据
BookService bookService=new BookServiceImpl();
List<Book> list=bookService.getBooksByCondition(categoryName);
//转换json
String json=JSON.toJSONString(list);
//显示数据
System.out.println("这是searchBook");
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.book.service.impl;
import java.util.ArrayList;
import java.util.List;
import com.book.domain.Book;
import com.book.service.BookService;
public class BookServiceImpl implements BookService {
//描述图书信息
private static final List<Book> books=new ArrayList<>();
public static List<Book> getBooks() {
return books;
}
@Override
//添加图书信息
public void addBook(Book book) {
boolean b=true;
for(Book bo:books) {
if(bo.getBookId().equals(book.getBookId())) {
b=false;
break;
}
}
if(b==true) {
books.add(book);
}
}
@Override//修改图书信息
public void updateBook(Book book) {
for(int i=0;i<books.size();i++) {
if((books.get(i).getBookId()).equals(book.getBookId())) {
books.set(i, book);
break;
}
}
}
@Override//删除图书信息
public void deleteBook(String bookId) {
for(Book book:books) {
if(book.getBookId().equals(bookId)) {
books.remove(book);
break;
}
}
}
@Override//根据图书ID来查询书籍信息
public Book getBooksBybookID(String bookID) {
//Book bo=new Book();
for(Book book:books) {
if(book.getBookId().equals(bookID)) {
return book;
}
}
return null;
}
@Override//根据分类来查询书籍信息
public List<Book> getBooksByCondition(String categoryName) {
List<Book> book1=new ArrayList<>();
for(Book book:books) {
if(book.getCategoryName().equals(categoryName)) {
book1.add(book);
}
}
return book1;
}
}
package com.book.service;
import java.util.List;
import com.book.domain.Book;
public interface BookService {
//添加图书信息
public void addBook(Book book);
//修改图书信息
public void updateBook(Book book);
//删除图书信息
public void deleteBook(String bookId);
//根据图书ID来查询书籍信息
public Book getBooksBybookID(String bookID);
//根据分类来查询书籍信息
public List<Book> getBooksByCondition(String categoryName);
}
正在回答
同学你好:同学应该是没有写tr标签,同学添加tr标签试试:例如你可以这样写一下
var query = "";
for (var i = 0; i < json.length; i++) {
query = query + "<tr id='tr1'><td>" + (i+1)
+ "</td><td>" ++json[i].bookId
+ "</td><td>" + json[i].bookName
+ "</td><td>" + json[i].categoryName
+ "</td><td>¥" + json[i].price
+ "</td><td>" + "<img src='json[i].path'>"
+ "</td></tr>";
//$("#tbody").remove();
}
$("#tbody").html(query);
如果我的回答解决了你的问题,请采纳,祝学习愉快.
package com.book.servlet;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
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 org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import com.book.domain.Book;
import com.book.service.BookService;
import com.book.service.impl.BookServiceImpl;
import com.book.utils.UploadUtils;
/**
* Servlet implementation class AddBook
*/
@WebServlet("/AddBook")
public class AddBook extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public AddBook() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//创建Map集合用于保存数据
Map<String ,String> map=new HashMap<>();
//1.创建磁盘工程
DiskFileItemFactory diskFile=new DiskFileItemFactory();
//2.创建核心解析类
ServletFileUpload fileUpload=new ServletFileUpload(diskFile);
//3.解析请求对象,将请求分成几个部分
try {
List<FileItem> list=fileUpload.parseRequest(request);
//4.遍历遍历集合获得每个部分的对象
for(FileItem fileItem:list) {
//判断是普通项还是文件上传项
if(fileItem.isFormField()) {
//获取普通项的名称
String name=fileItem.getFieldName();
//获取普通项的值
String value=fileItem.getString("utf-8");
//存储数据进map
map.put(name, value);
}else {
//文件上传项
//获取文件名称
String fileName=fileItem.getName();
String uuidFileName=UploadUtils.getUuidFileName(fileName);
//获取文件输入流
InputStream is=fileItem.getInputStream();
//将文件写入到服务器
String path=getServletContext().getRealPath("/img");
//创建文件输出流与文件输入流对接
String uri=path+"\\"+uuidFileName;
map.put("path", request.getContextPath()+"/img/"+uuidFileName);
OutputStream os=new FileOutputStream(uri);
int len =0;
byte[] b=new byte[1024];
while((len=is.read(b))!=-1) {
os.write(b, 0, len);
}
is.close();
os.close();
}
}
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//获取数据
String bookId=map.get("bookId");
String bookName=map.get("bookName");
String categoryId=map.get("categoryId");
//String id=null;
if(categoryId.equals("ca0001")) {
categoryId="计算机类";
}else if(categoryId.equals("ca0002")) {
categoryId="文学类";
}else if(categoryId.equals("ca0003")) {
categoryId="历史类";
}
//System.out.println(id);
String path=map.get("path");
String bookPrice=map.get("bookPrice");
String remarks=map.get("remarks");
//封装数据
Book book=new Book(bookId,bookName,categoryId,bookPrice,remarks,path);
//System.out.println(book);
//处理数据
BookService bookservice=new BookServiceImpl();
bookservice.addBook(book);
//显示结果
System.out.println("新的"+BookServiceImpl.getBooks());
request.getServletContext().setAttribute("books", BookServiceImpl.getBooks());
response.sendRedirect(request.getContextPath()+"/bookList.jsp");
}
/**
* @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 contentType="text/html;charset=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/bootstrap.min.css">
<link rel="stylesheet" href="css/add.css">
<script type="text/javascript">
function add(){
//定义分类ID的正则表达式
var regex=/^(ca)\d{4}$/;
//获取分类ID与分类名称
var categoryId=document.getElementById("categoryId").value;
var categoryName=document.getElementById("categoryName").value;
//判断分类ID是否符合要求
if(categoryId==null||categoryName==null||categoryId==""||categoryName==""){
alert("分类ID或分类名称不能为空,");
return false;
}else if(regex.test(categoryId)==false){
alert("分类ID不匹配,请重新输入");
return false;
}else{
return true;
}
}
</script>
</head>
<body>
<nav class="navbar navbar-default">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="">
图书分类管理
</a>
</div>
</div>
</nav>
<div class="container">
<div class="jumbotron">
<h1>Hello, ${sessionScope.user.userName }</h1>
<p>请小心地新增图书分类,要是建了一个错误的就不好了。。。</p>
</div>
<div class="page-header">
<h3><small>新建</small></h3>
</div>
<form class="form-horizontal" action="${pageContext.request.contextPath }/AddCategory" method="post" onsubmit="return add()">
<div class="form-group">
<label for="name" class="col-sm-2 control-label">分类ID :</label>
<div class="col-sm-8">
<input name="categoryId" class="form-control" id="categoryId">
</div>
</div>
<div class="form-group">
<label for="name" class="col-sm-2 control-label">分类名称 :</label>
<div class="col-sm-8">
<input name="categoryName" class="form-control" id="categoryName">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary">保存</button>
</div>
</div>
</form>
</div>
<footer class="text-center" >
copy@imooc
</footer>
</body>
</html>
这个是添加图书的
- 参与学习 人
- 提交作业 676 份
- 解答问题 9666 个
本阶段将从前端网页搭建入手,到Java Web基础,前后端结合助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星