下拉框继续显示${c.categoryName }
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false"%>
<%@ 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="${pageContext.request.contextPath}/SearchBookServlet" 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" onclick="query()">查询</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="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 }/UpdateBookServlet?bookId=${book.bookId}">修改</a> <a
href="${pageContext.request.contextPath }/DeleteBookServlet?bookId=${book.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.imooc.servlet;
import java.io.IOException;
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.BookService;
import com.imooc.web.service.impl.BookServiceImp;
@WebServlet("/UpdateBookServlet")
public class UpdateBookServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public UpdateBookServlet() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//接收用户信息
String bookId=request.getParameter("bookId");
BookService bookService=new BookServiceImp();
Book book=bookService.getBooksById(bookId);
request.setAttribute("book", book);
System.out.println(book);
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" isELIgnored="false"%>
<%@ 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>
</div>
</div>
</form>
</div>
<footer class="text-center" >
copy@imooc
</footer>
</body>
</html>
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.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);
return;
}
}
}
// 根据图书id查询图书
@Override
public Book getBooksById(String bookId) {
System.out.println("id:"+bookId+"size:"+books.size()+" "+books);
for(Book b:books) {
if(b.getBookId().equals(bookId)) {
return b;
}
}
return null;
}
//根据分类查询书籍信息
@Override
public List<Book> getBooksByCatgoryName(String categoryName) {
List<Book> bookList=new ArrayList<>();
for(Book b:books){
if(b.getCategoryName().equals(categoryName)){
bookList.add(b);
}
}
return bookList;
}
}
添加了
isELIgnored="false"
下拉框显示${c.categoryName } ,还有修改还是无法覆盖,又显示多一行
正在回答 回答被采纳积分+1
相似问题
登录后可查看更多问答,登录/注册
- 参与学习 人
- 提交作业 676 份
- 解答问题 9666 个
本阶段将从前端网页搭建入手,到Java Web基础,前后端结合助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星