修改图书的问题
来源:6-2 项目作业
邢文凯888
2019-11-06 21:15:25
怎么在修改图书页面显示id呀,那个只读的怎么显示
修改图书方法具体实现原理是怎么实现的
<%@ 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="${.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="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.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 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();
}
//封装数据
Book book = new Book();
book.setBookId("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);
}
}
<%@ 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 }/updateBook.jsp">修改</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>
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;
}
}
}
//修改图书的方法
public void updateBook(Book book) {
deletebook(book.getBookId());
books.add(book);
}
}
1回答
好帮手慕阿满
2019-11-07
同学你好,在展示图书页面,修改图书的链接上,建议获取当前图片的id,并跳转到一个servlet中,在servlet中通过图书id获取图书信息并存入request中,转发到修改图书的页面。例如:
关于只读,同学可以在显示id的input中,增加readonly="readonly",表示字段为只读字段,不能修改。
如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~
相似问题