这是什么原因啊老师帮忙看一下,谢谢

这是什么原因啊老师帮忙看一下,谢谢

package com.imooc.web.service.impl;

import java.util.ArrayList;

import java.util.List;

import com.imooc.domain.Book;

import com.imooc.domain.Category;

import com.imooc.web.service.BookService;

public class BookServiceImp implements BookService {

private static final List<Book> books = new ArrayList<Book>();

    

public static List<Book> getBooks(){

return books;

}

// 添加图书

@Override

public void addBook(Book book) { 

// 在books集合列表中查找,==-1 ,如果没有

if(books.indexOf(book)==-1) {

books.add(book);

}else {

System.out.println("该书籍已经存在");

return;

}

}

    

// 修改图书

@Override

public void updateBook(Book book) {

       for(Book bk:books) {

      if(bk.getBookId().equals(book.getBookId())) {

      bk.setBookName(book.getBookName());

      bk.setBookPrice(book.getBookPrice());

      bk.setCategoryName(book.getCategoryName());

      bk.setRemarks(book.getRemarks());

      bk.setBookPic(book.getBookPic());

      }

       }

}

   //删除图书

@Override

public void deleteBook(String bookId) {

for(Book book:books) {

if(book.getBookId().equals(bookId)) {

books.remove(book);

break;

}

}

}

    // 根据图示id查询图书

@Override

public Book getBooksById(String bookID) {

// TODO Auto-generated method stub

return null;

}

 

//根据分类查询书籍信息

@Override

public List<Book> getBooksByCatgoryName(String catgoryName) {

// TODO Auto-generated method stub

return null;

}

}



package com.imooc.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.imooc.domain.Book;

import com.imooc.web.service.impl.BookServiceImp;


@WebServlet("/AddBookServlet")

public class AddBookServlet extends HttpServlet {

private static final long serialVersionUID = 1L;


public AddBookServlet() {

super();


}


protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {


// 创建map用于保存用户数据

Map<String,String> map = new HashMap<String,String>();

// 创建磁盘工厂

DiskFileItemFactory diskfileitemfactory = new DiskFileItemFactory();

// 解析核心类

ServletFileUpload fileUpload = new ServletFileUpload(diskfileitemfactory);


// 请求解析对象,将请求分成几个部分(FileItem)

// 4. 遍历集合获得每个部分的对象

try {

List<FileItem> list = fileUpload.parseRequest(request);

for (FileItem fileitem : list) {

// 判断是普通项还是文件上传项

if (fileitem.isFormField()) {

// 普通项

// 获得普通项名称

String name = fileitem.getFieldName();

// 获得普通项的值

String value = fileitem.getString("UTF-8");

// 保存数据

map.put(name, value);

} else {

String fileName = fileitem.getName();

// 获得文件输入流

InputStream ips = fileitem.getInputStream();

// 将文件写入到服务器

String path = getServletContext().getRealPath("/upload");

// 创建文件输出流,与输入流进行对接

String url = path + "/" + fileName;

map.put("path", request.getContextPath() + "/upload/" + fileName);

OutputStream ops = new FileOutputStream(url);

int len = 0;

byte[] b = new byte[1024];

while ((len = ips.read(b)) != -1) {

ops.write(b, 0, len);

}

ips.close();

ops.close();

}

}

} catch (FileUploadException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

// 封装数据

Book book = new Book();

book.setBookId(map.get("bookId"));

book.setBookName(map.get("bookName"));

book.setCategoryName(map.get("categoryName"));

book.setBookPrice(map.get("bookPrice"));

book.setBookPic(map.get("path"));

book.setRemarks(map.get("remarks"));


// 处理数据

BookServiceImp booksrevice = new BookServiceImp();

booksrevice.addBook(book);

request.getSession().setAttribute("bookList", booksrevice.getBooks());

System.out.println(booksrevice.getBooks());

// 显示结果

request.getRequestDispatcher("/bookList.jsp").forward(request, response);


}


protected void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

// TODO Auto-generated method stub

doGet(request, response);

}


}

package com.imooc.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.imooc.domain.Book;

import com.imooc.web.service.impl.BookServiceImp;



@WebServlet("/UpdateBook")

public class UpdateBook extends HttpServlet {

private static final long serialVersionUID = 1L;

       

   

    public UpdateBook() {

        super();

        // TODO Auto-generated constructor stub

    }



protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

  //接收用户信息

  String bookId = request.getParameter("bookId");

   

  Book book = new Book();

  book.setBookId(bookId);

   

  BookServiceImp  bk= new BookServiceImp();

  bk.updateBook(book);

  //处理用户信息

  request.getServletContext().setAttribute("bookList",bk.getBooks());

   

  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);

}


}

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<!DOCTYPE >

<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" src="js/jquery-3.3.1.min.js"></script>

<script type="text/javascript">

function checkForm() {

var id = $("#bookId").val();

var name = $("#bookName").val();

var categoryId = $("#categoryId").val();

var price = $("#bookPrice").val();

var remarks = $("#remarks").val();

var regex = /(^[1-9]\d*(\.\d{1,2})?$)|(^0(\.\d{1,2})?$)/;

if (id == "" || id == null) {

alert("编号不能为空");

return false;

} else if (name == "" || name == null) {

alert("书名不能为空");

return false;

} else if (categoryId == "" || categoryId == null) {

alert("请先添加分类");

return false;

} else if (price == "" || price == null) {

alert("价格不能为空");

return false;

} else if (regex.test(price) == false) {

alert("价格格式不正确!");

return false;

} else if (remarks == "" || remarks == null) {

alert("备注不能为空");

return false;

}

alert("图书添加成功");

return true;

}

</script>

</head>

<body>

<nav class="navbar navbar-default">

<div class="container">

<div class="navbar-header">

<a class="navbar-brand" href="/dept/list.do"> 图书信息管理 </a>

</div>

</div>

</nav>

<div class="container">

<div class="jumbotron">

<h1>Hello, XXX!</h1>

<p>请小心地新增图书信息,要是建了一个错误的就不好了。。。</p>

</div>

<div class="page-header">

<h3>

<small>新建</small>

</h3>

</div>

<form class="form-horizontal"

action="${pageContext.request.contextPath }/AddBookServlet"

method="post" onsubmit="return checkForm()"

enctype="multipart/form-data">


<div class="form-group">

<label for="name" class="col-sm-2 control-label">图书编号 :</label>

<div class="col-sm-8">

<input name="bookId" class="form-control" id="bookId">

</div>

</div>

<div class="form-group">

<label for="name" class="col-sm-2 control-label">图书名称 :</label>

<div class="col-sm-8">

<input name="bookName" class="form-control" id="bookName">

</div>

</div>

<div class="form-group">

<label for="categoryId" class="col-sm-2 control-label">分类 :</label>

<select id="categoryId" name="categoryName"

class="col-sm-2 form-control"

style="width: auto; margin-left: 15px">

<c:forEach items="${sessionScope.cateList }" var="c"

varStatus="idx">

<option value="${c.categoryName }" selected="selected">{c.categoryName

}</option>

</c:forEach>

<!-- 下拉列表的内容要从分类中进行读取,value值是分类id -->

</select>

</div>


<div class="form-group">

<label for="name" class="col-sm-2 control-label">价格 :</label>

<div class="col-sm-8">

<input name="bookPrice" class="form-control" id="bookPrice">

</div>

</div>


<div class="form-group">

<label for="name" class="col-sm-2 control-label">图书封面 :</label> <input

type="file" id="bookPic" name="bookPic" style="padding-left: 15px">

</div>


<div class="form-group">

<label for="name" class="col-sm-2 control-label">备注 :</label>

<div class="col-sm-8">

<input name="remarks" class="form-control" id="remarks">

</div>

</div>


<div class="form-group">

<div class="col-sm-offset-2 col-sm-10">

<button type="submit" class="btn btn-primary">保存</button>

&nbsp;&nbsp;&nbsp;

</div>

</div>

</form>

</div>

<footer class="text-center"> copy@imooc </footer>

</body>

</html>


<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<!DOCTYPE >

<html>

    <head>

        <meta charset="UTF-8">

        <title>修改图书信息</title>

        <link rel="stylesheet" href="css/bootstrap.min.css">

        <link rel="stylesheet" href="css/add.css">

    </head>

    <body>

        <nav class="navbar navbar-default">

            <div class="container">

                <div class="navbar-header">

                    <a class="navbar-brand" href="/dept/list.do">

                        图书信息管理

                    </a>

                </div>

            </div>

        </nav>

        <div class="container">

            <div class="jumbotron">

                <h1>Hello, XXX!</h1>

                <p>请小心的修改图书信息。。。</p>

            </div>

            <div class="page-header">

                <h3><small>修改</small></h3>

            </div>

            <form class="form-horizontal"enctype="multipart/form-data" action="${pageContext.request.contextPath }/AddBookServlet" method="post">


                <div class="form-group">

                    <label for="name" class="col-sm-2 control-label">图书编号 :</label>

                    <div class="col-sm-8">

                        <input name="bookId" class="form-control" id="bookId" readonly="readonly"value="${requestScope.book.bookId }">

                    </div>

                </div>

                <div class="form-group">

                    <label for="name" class="col-sm-2 control-label">图书名称 :</label>

                    <div class="col-sm-8">

                        <input name="bookName" class="form-control" id="bookName"value="${requestScope.book.bookName }">

                    </div>

                </div>

                <div class="form-group">

                    <label for="categoryId" class="col-sm-2 control-label">分类 :</label>

                    <select id="categoryId" name="categoryId" class="col-sm-2 form-control" style="width: auto;margin-left: 15px">

                       <c:forEach items="${sessionScope.cateList }" var="c"

varStatus="idx">

<option value="${c.categoryName }" selected="selected">{c.categoryName

}</option>

</c:forEach>

                       <!-- 下拉列表的内容要从分类中进行读取,value值是分类id -->

                    </select>

                </div>


                 <div class="form-group">

                    <label for="name" class="col-sm-2 control-label">价格 :</label>

                    <div class="col-sm-8">

                        <input name="bookPrice" class="form-control" id="bookPrice">

                    </div>

                  </div>

                   

                  <div class="form-group" >

                    <label for="name" class="col-sm-2 control-label">图书封面 :</label>

                    <input type="file" id="bookPic" name="bookPic" style="padding-left: 15px">

                  </div>


                  <div class="form-group">

                    <label for="name" class="col-sm-2 control-label">备注 :</label>

                    <div class="col-sm-8">

                        <input name="remarks" class="form-control" id="remarks">

                    </div>

                  </div>


                <div class="form-group">

                    <div class="col-sm-offset-2 col-sm-10">

                        <button type="submit" class="btn btn-primary">修改</button>&nbsp;&nbsp;&nbsp;

                    </div>

                </div>

            </form>

        </div>

        <footer class="text-center" >

            copy@imooc

        </footer>

    </body>

</html>


<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<!DOCTYPE>

<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="/searchBook" method="post">

<div class="form-group" style="float: right;">

<div class="col-sm-offset-2 col-sm-10">

<button type="submit" class="btn btn-primary">查询</button>

&nbsp;&nbsp;&nbsp;

</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>

<c:forEach items="${sessionScope.bookList}" var="book"

varStatus="idx">

<tr id="tr1">

<td>${idx.index+1}</td>

<td>${book.bookId}</td>

<td>${book.bookName}</td>

<td>${book.categoryName}</td>

<td>¥${book.bookPrice}</td>

<td><img src="${book.bookPic }"></td>

<td><a href="${pageContext.request.contextPath }/UpdateBook?bookId=${book.bookId}">修改</a> <a

href="/deleteBook?bookId=book0001">删除</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>

两个问题

第一个:为什下拉框无法显示中文啊,只显示

${c.categoryName }

第二个:修改图书回出现新的一行,无法覆盖,如图

http://img1.sycdn.imooc.com//climg/5f02c660099cb4ec13140617.jpghttp://img1.sycdn.imooc.com//climg/5f02c6820958d7fe10480432.jpg

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

2回答
好帮手慕阿满 2020-07-06 19:01:31

我在https://class.imooc.com/course/qadetail/238158这个问答中回复了你,请前去查看。

祝:学习愉快~

好帮手慕阿满 2020-07-06 16:44:25

同学你好,关于同学的问题:

1、可能是不能识别el表达式,建议在jsp页面的头部增加如下代码:

<%@ page isELIgnored="false" %>

2、修改图书有点问题,按照同学的代码,相当于新增了一个id为原来图书Id的,其他信息都为空的图书。所以点击修改出现新的一行。

这里应该是bookList.jsp页面中,点击修改后,跳转到servlet中,获取该bookId对应的图书信息,然后跳转update.jsp页面中显示。在update.jsp中修改图书信息后,跳转到Servlet中,调用修改图书的方法修改图书信息。

建议同学按照上述步骤重新完成代码。

祝:学习愉快~

  • 提问者 J_DP #1
    第一个按老师的添加进去还是没有用,第二个问题写了但是还是出现这个问题,麻烦老师指点下具体是哪里出错了吗,我有点迷糊
    2020-07-06 18:20:02
  • 提问者 J_DP #2
    https://class.imooc.com/course/qadetail/238158 老师帮忙移驾看一下
    2020-07-06 18:24:56
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师