java.lang.NumberFormatException问题
老师麻烦看下,这是什么问题
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.service.impl.BookServiceImpl;
import com.imooc.service.impl.CategoryServiceImpl;
import com.imooc.utils.UploadCopy;
@WebServlet("/UpdateBookServlet")
public class UpdateBookServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//接受数据
//创建Map集合用于保存数据:
Map<String,String> map=new HashMap<>();
// 1.创建磁盘文件项工厂
DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
// 2.创建核心解析类
ServletFileUpload fileUpload = new ServletFileUpload(diskFileItemFactory);
// 3.解析请求对象,将请求分成几个部分(FileItem)
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.put(name, value);
} else {
// 文件上传项
// 获得文件的名称:
String fileName = fileItem.getName();
if(fileName!=null&&!fileName.equals("")) {
//获得唯一文件名:
String uuidFileName=UploadCopy.getUniqueFileName(fileName);
// 获得文件的输入流:
InputStream is = fileItem.getInputStream();
// 需要将文件写到服务器的某个路径:
String path = getServletContext().getRealPath("/upload");
// 显示图片<img src="/bookManage/upload/*.jpg">
map.put("bookPicture",request.getContextPath()+"/upload/"+uuidFileName);
// 创建输出流与输入流进行对接:
String url=path + "\\" + uuidFileName;
OutputStream os = new FileOutputStream(url);
int len = 0;
byte[] b = new byte[1024];
while ((len = is.read(b)) != -1) {
os.write(b, 0, len);
}
os.close();
is.close();
}
}
}
} catch (FileUploadException e) {
e.printStackTrace();
}
//封装数据:
//更新指定id的图书信息
BookServiceImpl bsi=new BookServiceImpl();
CategoryServiceImpl csi=new CategoryServiceImpl();
Book book=new Book();
String categoryId=map.get("categoryId");
String categoryName=csi.getCategoryName(categoryId);
book.setBookId(map.get("bookId"));
book.setBookName(map.get("bookName"));
book.setCategoryName(categoryName);
book.setBookPrice(map.get("bookPrice"));
book.setBookPicture(map.get("bookPicture"));
book.setBookMore(map.get("bookMore"));
request.setAttribute("book", book);
bsi.updateBook(book);
//处理数据:
//更新会话中的图书列表
List<Book> bookList=bsi.getBooks();
request.getSession().removeAttribute("bookList");
request.getSession().setAttribute("bookList", bookList);
//显示结果:
request.getRequestDispatcher("/bookList.jsp").forward(request, response);
// response.sendRedirect(request.getContextPath()+"/bookList.jsp");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
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>
<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="${pageContext.request.contextPath}/bookList.jsp">
图书信息管理
</a>
</div>
</div>
</nav>
<div class="container">
<div class="jumbotron">
<h1>Hello, ${newUser.getUsername()}!</h1>
<p>请小心的修改图书信息。。。</p>
</div>
<div class="page-header">
<h3><small>修改</small></h3>
</div>
<form class="form-horizontal" action="${pageContext.request.contextPath}/UpdateBookServlet" enctype="multipart/form-data" 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">
</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=${bookList.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.categoryList}" var="cl">
<option value="${cl.categoryId}" selected="selected">${cl.categoryName}</option>
</c:forEach>
</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>
</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 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="/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>
</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="bl" varStatus="idx">
<tr id="tr1">
<td>${idx.index+1}</td>
<td>${bl.bookId}</td>
<td>${bl.bookName}</td>
<td>${bl.categoryName}</td>
<td>¥${bl.bookPrice}</td>
<td><img src="${bl.bookPicture}"></td>
<td>
<a href="${pageContext.request.contextPath}/updateBook.jsp?bookId=${bl.bookId}">修改</a>
<a href="${pageContext.request.contextPath}/DeleteBookServlet?bookId=${bl.bookId}">删除</a>
</td>
</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.imooc.domain;
public class Book {
private String bookId;
private String bookName;
private String categoryName;
private String bookPrice;
private String bookPicture;
private String bookMore;
public Book() {
super();
}
public Book(String bookId, String bookName, String categoryName, String bookPrice, String bookPicture,
String bookMore) {
super();
this.bookId = bookId;
this.bookName = bookName;
this.categoryName = categoryName;
this.bookPrice = bookPrice;
this.bookPicture = bookPicture;
this.bookMore = bookMore;
}
public String getBookId() {
return bookId;
}
public void setBookId(String bookId) {
this.bookId = bookId;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getCategoryName() {
return categoryName;
}
public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}
public String getBookPrice() {
return bookPrice;
}
public void setBookPrice(String bookPrice) {
this.bookPrice = bookPrice;
}
public String getBookPicture() {
return bookPicture;
}
public void setBookPicture(String bookPicture) {
this.bookPicture = bookPicture;
}
public String getBookMore() {
return bookMore;
}
public void setBookMore(String bookMore) {
this.bookMore = bookMore;
}
@Override
public String toString() {
return "Book [bookId=" + bookId + ", bookName=" + bookName + ", categoryName=" + categoryName + ", bookPrice="
+ bookPrice + ", bookPicture=" + bookPicture + ", bookMore=" + bookMore + "]";
}
}
正在回答
同学你好,根据同学的描述图书的编号就丢失以及修改后增加一列,是因为同学的思路编写有点小问题。这里建议 :
将修改页面对应的地址跳转到一个Servlet中,将这个id信息可以通过request传递到对应的修改的jsp页面中。
老师这里举个例子:
然后应该是在修改页面中,填入数据,跳转到修改的Servlet中,再进行获取全部的书籍,然后获得书籍ID,通过ID查找书籍,集合里移除原书籍,最后进行修改书籍信息。
例如:
如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~
老师,发现什么问题了,已经解决了
但现在,为什么修改信息的时候,编号一栏不能输入呢?
- 参与学习 人
- 提交作业 676 份
- 解答问题 9666 个
本阶段将从前端网页搭建入手,到Java Web基础,前后端结合助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星