老师,为什么我点击了新建,也获取到了数据,就是不显示呢,而且怎么分类没有获取到信息呢
package com.imooc.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.imooc.domain.Book;
import com.imooc.impl.BookServiceImpl;
import com.imooc.service.BookService;
import com.imooc.utils.UploadUtils;
@WebServlet("/AddBookServlet")
public class AddBookServlet 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<String,String>();
//文件上传代码:
//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()) {
//普通项--用户名(username--输入的值) 密码
//获得普通方向的名称:
String name=fileitem.getFieldName();
//获得普通项的值
String value=fileitem.getString("UTF-8");
//保存数据
map.put(name,value);
}else {
//文件上传项
//获得文件的名称:
String fileName=fileitem.getName();
//获得唯一文件名:
String uuidFileName=UploadUtils.getUuidFileName(fileName);
//获得文件的输入流:
InputStream is=fileitem.getInputStream();
//需要将文件写入到服务器的某个路径即可:
String path=getServletContext().getRealPath("/upload");
System.out.println(path);//D:/xxx/ddd/
//显示图片<img src="/regist_login_xm/a.jpg">
//创建输出流与输入流进行对接:
String url=path+"\\"+uuidFileName;
map.put("fengmian",request.getContextPath()+"/upload"+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);
}
is.close();
os.close();
}
}
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// //接收数据
// String bid = request.getParameter("bookId");
// String bname = request.getParameter("bookName");
// String price = request.getParameter("bookPrice");
// String fl=request.getParameter("categoryId");
// String bz = request.getParameter("remarks");
//封装数据
Book book=new Book();
book.setBid(map.get("bookId"));
book.setBname(map.get("bookName"));
Map<String,String> categoryMap =(Map<String, String>) getServletContext().getAttribute("categoryMap");
book.setBcategory(categoryMap.get("categoryId"));
book.setFengmian(map.get("fengmian"));
book.setPrice(map.get("bookPrice"));
book.setBz(map.get("remarks"));
System.out.println(book);
//处理数据
BookService bookService=new BookServiceImpl();
bookService.addBook(book);
List<Book> bookList = (List)request.getServletContext().getAttribute("bookList");
response.sendRedirect(request.getContextPath()+"/bookList.jsp");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
package com.imooc.web.listener;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import com.imooc.domain.Book;
import com.imooc.impl.BookServiceImpl;
@WebListener
public class BookListener implements ServletContextListener {
public BookListener() {
}
public void contextDestroyed(ServletContextEvent arg0) {
}
public void contextInitialized(ServletContextEvent sce) {
List<Book> bookList=BookServiceImpl.getBooks();
sce.getServletContext().setAttribute("bookList", bookList);
Map<String,String> categoryMap=new HashMap<String,String>();
sce.getServletContext().setAttribute("categoryMap", categoryMap);
}
}
<%@ 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="${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" id="search">查询</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 id="cont">
<c:forEach items="${bookList }" var="b" varStatus="bookStatus">
<tr id="tr1">
<td>${bookStatus.index +1 }</td>
<td>${b.bid }</td>
<td>${b.bname }</td>
<td>${b.bcategory }</td>
<td>¥${b.price }</td>
<td><img src="${b.fengmian }"></td>
<td>
<a href="/updateBook?bookId=book0001">修改</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>
<script type="text/javascript" src="js/jquery-3.3.1.js" ></script>
<script type="text/javascript">
$("#search").click(function(){
$.ajax({
"url":"${pageContext.request.contextPath}/SearchBookServlet",
"type":"post",
"data":{'id':$("searchContent").val()},
"dataType":"json",
"success":function(json){
var content="";
for(var i=0;i<json.length;i++){
content=content+ "<tr><td>"
+ (i + 1)
+ "</td><td>"
+ json[i].bookId
+ "</td><td>"
+ json[i].bName
+ "</td><td>"
+ json[i].category
+ "</td><td>"
+ json[i].price
+ "</td><td> <a href='${pageContext.request.contextPath}/updateBook.jsp?bookId="
+ json[i].bookId
+ "'>修改</a><a href='${pageContext.request.contextPath}/DeleteBookServlet?bookId="
+ json[i].bookId + "'>删除</a></td></tr>";
$("#cont>tr").remove();
}
$("#cont").html(content);
}
});
});
</script>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!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="/dept/add.do" 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">
</div>
</div>
<div class="form-group">
<label for="categoryName" class="col-sm-2 control-label">分类 :</label>
<select id="categoryName" name="categoryName" class="col-sm-2 form-control" style="width: auto;margin-left: 15px">
<option value="ca0001" selected="">计算机</option>
<option value="ca0002">文学</option>
<option value="ca0003">历史</option>
<!-- 下拉列表的内容要从分类中进行读取,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>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!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" 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">
</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">
<option value="ca0001" selected="">计算机</option>
<option value="ca0002">文学</option>
<option value="ca0003">历史</option>
<!-- 下拉列表的内容要从分类中进行读取,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>
正在回答
同学你好,分类显示不出来是因为同学在获取分类时代码有误,具体修改如下图:
同时,在前台页面展示分类信息时,不应该显示分类ID而是应该显示分类名称,具体修改如下图:
addBook.jsp
将value的值更改为分类名称即可。
如果我的回答解决了你的疑惑,请采纳!祝学习愉快~
- 参与学习 人
- 提交作业 676 份
- 解答问题 9666 个
本阶段将从前端网页搭建入手,到Java Web基础,前后端结合助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星