修改图书
这个修改图书怎么写呀不会了
没有头绪呀,求给一个准确的方法
<%@ 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.html" >图书信息管理</a>
</nav>
<nav>
<a href="categoryList.html" >分类管理</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="p" varStatus="idx">
<tr id="tr1">
<td>${idx.index + 1 }</td>
<td>${p.bookId }</td>
<td>${p.bookName }</td>
<td>${p.categoryId}</td>
<td>${p.bookPrice}</td>
<td><img src="${p.bookPic}"></td>
<td>
<a href="${pageContext.request.contextPath }/xiugaibook?bookId=${p.bookId}">修改</a>
<a href="${pageContext.request.contextPath }/DeletebookServlet?bookId=${p.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>
<%@ 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/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" action="${pageContext.request.contextPath }/updateBookservlet" 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="">
</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="categoryId" class="col-sm-2 form-control" style="width: auto;margin-left: 15px">
<c:forEach items="${sessionScope.bookcategorylist }" var="p" varStatus="idx">
<option value="${p.categoryId }">${p.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>
</div>
</div>
</form>
</div>
<footer class="text-center" >
copy@imooc
</footer>
</body>
</html>
package com.xing.web.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.xing.domain.Book;
import com.xing.service.lmpl.addBookSerice;
/**
* Servlet implementation class xiugaibook
*/
@WebServlet("/xiugaibook")
public class xiugaibook extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String bookId = request.getParameter("bookId");
addBookSerice addbook = new addBookSerice();
List<Book> listbook = addbook.idbook(bookId);
request.getSession().setAttribute("listbook", listbook);
response.sendRedirect(request.getContextPath()+"/updateBook.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);
}
}
package com.xing.web.servlet;
import java.io.File;
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 javax.websocket.Session;
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.xing.domain.Book;
import com.xing.service.lmpl.addBookSerice;
import com.xing.utils.uploadutils;
/**
* Servlet implementation class updateBookservlet
*/
@WebServlet("/updateBookservlet")
public class updateBookservlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Map<String, String> map =new HashMap<String, String>();
DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
ServletFileUpload fileUpload = new ServletFileUpload(diskFileItemFactory);
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();
String uuidfiename = uploadutils.getuuidfilename(fileName);
InputStream is=fileItem.getInputStream();
String path = getServletContext().getRealPath("/upload");
String url = path+"\\"+uuidfiename;
map.put("bookPic", request.getContextPath()+"/upload/"+uuidfiename);
//如果文件不存在。则创建一个
if(!new File(path).exists()) {
new File(path).mkdirs();
}
OutputStream os = new FileOutputStream(url);
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();
}
//封装数据
addBookSerice addbook = new addBookSerice();
Book book = new Book();
String bookId = addbook.id(bookId)
book.setBookName(map.get("bookName"));
book.setCategoryId(map.get("categoryId"));
book.setBookPrice(map.get("bookPrice"));
book.setBookPic(map.get("bookPic"));
book.setRemarks(map.get("remarks"));
addBookSerice addbook = new addBookSerice();
addbook.updateBook(book);
List<Book> booklist = addbook.getbook();
request.getSession().setAttribute("booklist", booklist);
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);
}
}
package com.xing.service.lmpl;
import java.util.ArrayList;
import java.util.List;
import com.xing.domain.BookCategory;
public class CategoryServicelmpl {
public static List<BookCategory> bookcategory = new ArrayList<BookCategory>();
//添加图书分类
public void addCatgory(String categoryId,String categoryName) {
BookCategory bc= new BookCategory(categoryId,categoryName);
bookcategory.add(bc);
}
//获取所有分类
public List<BookCategory> getCategorys(){
return bookcategory;
}
//删除图书分类
public void deleteCatgory(String categoryId) {
for(BookCategory boca:bookcategory) {
if(boca.getCategoryId().equals(categoryId)) {
bookcategory.remove(boca);
break;
}
}
}
}
package com.xing.service.lmpl;
import java.util.ArrayList;
import java.util.List;
import com.xing.domain.Book;
public class addBookSerice {
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(String bookId) {
for(Book bookk:books){
if(bookk.getBookId().equals(bookId)) {
books.remove(bookk);
break;
}
}
}
//通过id获取图书信息
public List<Book> idbook(String bookId) {
for(Book bookk:books) {
if(bookk.getBookId().equals(bookId)) {
return books;
}
}
return null;
}
//修改图书的方法
public void updateBook(Book book) {
deletebook(book.getBookId());
books.add(book);
}
public String id(String bookId) {
return null;
}
}
正在回答 回答被采纳积分+1
同学你好。
1、因为之前在xiugaibook这个Servlet中我们已经获取了当前的图书对象thisbook并将它放入了session中,所以可以在这里直接展示出来:
由于这里我们对input设置了readonly="readonly"。这个input的值就是只读的不可更改的,也就保证了后面在form提交之后,使用bookId获取的还是原来的id值。
thisbook对象中其余可以修改的项,也可以使用EL表达式将值展示出来,只是不限制readonly,放开修改权限。在form表单提交时,自然会将值含带过去。
2、在form表单提交的Servlet中,同学已经将普通表单项都按照name value 的形式填入了map中,那么可以在后面通过map.get("bookId")也可以获取id值。在组装Book对象的时候直接使用setBookId(map.get("bookId"))即可。
如果解答了同学的疑问,望采纳~
祝学习愉快~
同学你好,修改图书分两步:
1、第一步,要将需要修改的图书原信息展示出来,供人修改:
1)根据图书id获取图书,获取的应该是一本图书,而不是图书列表。我们想要展示的也是这一个的图书信息。
因此根据id获取图书直接将方法返回值改为book,在找到后直接将这个图书对象返回:
在对应的Servlet中也可以直接将修改前的thisbook信息保存到session中,以便页面使用:
对应的页面updateBook页面。从session中取出这个对象中的信息,展示出来。
2、第二步,要将修改后的信息替换原来的图书信息,保存到图书列表中,这部分同学的思路没什么大问题。
1)在updateBook页面,使用form将信息提交到Servlet中。
2)在Servlet中将信息组装成Book对象,进行更新。同学使用的这种先删除再添加的方式也是可以的。
如果解答了同学的疑问,望采纳~
祝学习愉快~
- 参与学习 人
- 提交作业 676 份
- 解答问题 9666 个
本阶段将从前端网页搭建入手,到Java Web基础,前后端结合助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星