显示图书管理页面 展示图书分类及各分类下图书的访问地址url书写老师指导一下
category.jsp:
<%@page contentType="text/html;charset=utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ 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/index.css">
<link rel="stylesheet" href="css/bootstrap.min.css">
</head>
<body>
<header>
<div class="container">
<c:forEach items="${categoryList}" var="category">
<%-- <#list categorylist as category>--%>
<nav>
<a href="/management?method=showPartBook&categoryId=${category.categoryId}" >${category.categoryName}</a>
</nav>
<%-- <#list>--%>
</c:forEach>
<nav>
<a href="${pageContext.request.contextPath}/category?method=showCategory" >分类</a>
</nav>
</div>
</header>
<section class="banner">
<div class="container">
<div>
<h1>图书</h1>
<p>图书分类列表</p>
</div>
</div>
</section>
<section class="main">
<div class="container">
<table class="table table-striped">
<thead>
<tr>
<th>名称</th>
<th>创建时间</th>
<th>最后修改时间</th>
</tr>
</thead>
<tbody>
<c:forEach items="${categoryList}" var="dept">
<tr>
<td>${dept.name}</td>
<td><fmt:formatDate value="${dept.createTime}" pattern="yyyy-MM-dd HH:mm:ss"></fmt:formatDate></td>
<td><fmt:formatDate value="${dept.updateTime}" pattern="yyyy-MM-dd HH:mm:ss"></fmt:formatDate></td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</section>
<section class="page">
<div class="container">
<div id="fatie">
<a href="/category?method=showCreatePage"><button>新建</button></a>
</div>
</div>
</section>
<footer>
copy@慕课网
</footer>
</body>
</html>
categoryServlet:
package com.imooc.oa.controller;
import com.alibaba.fastjson.JSON;
import com.imooc.oa.entity.Category;
import com.imooc.oa.service.CategoryService;
import com.imooc.oa.entity.Category;
import com.imooc.oa.service.CategoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.servlet.http.HttpSession;
import java.io.IOException;
import java.security.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;
@WebServlet(name = "CategoryServlet",urlPatterns = "/category")
public class CategoryServlet extends HttpServlet {
private CategoryService categoryService = new CategoryService();
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
String uri = req.getRequestURI();
String method = uri.substring(uri.lastIndexOf("/") + 1);
if (method.equals("createCategory")) {
this.createCategory(req, resp);
} else if (method.equals("showCreatePage")) {
this.showCreatePage(req, resp);
}
List<Category> categoryList = (List<Category>) categoryService.select();
req.setAttribute("categoryList", categoryList);
req.getRequestDispatcher("/WEB-INF/jsp/category.jsp").forward(req,resp);
}
public void createCategory(HttpServletRequest request,HttpServletResponse response) throws IOException {
// FileItemFactory factory=new DiskFileItemFactory();
// //将前端表单数据转换为FileItem对象
// ServletFileUpload sf=new ServletFileUpload(factory);
// //用于为FileUpload组件提供java web层面上的http请求解析,增加对web的支持。
// try {
// List<FileItem> formData = sf.parseRequest(request);
// Category category = new Category();
// for(FileItem fi : formData) {
// if(fi.isFormField()) {
// switch (fi.getFieldName()) { //用于处理普通文本框
// case "categoryId":
// category.setCategoryId(fi.getString("UTF-8"));
// break;
// case "categoryName":
// category.setCategoryName(fi.getString("UTF-8"));
// break;
//// case "createTime":
//// category.setCreateTime(fi.getString("UTF-8"));
//// break;
//// case "update"
// default:
// break;
// }
// }
// }
// categoryService.createCategory(category);//此为新增功能,层层调用,controller,service,xml。
// response.sendRedirect("/management?method=categoryList");//在填完表单之后返回
// }catch(FileUploadException e) {
// e.printStackTrace();
// }
String name=request.getParameter("name");
Category category=new Category();
category.setCategoryName(name);
category.setCreateTime(new Date(System.currentTimeMillis()));
category.setUpdateTime(new Date(System.currentTimeMillis()));
categoryService.createCategory(category);//此为新增功能,层层调用,controller,service,xml。
response.sendRedirect("/management?method=categoryList");
}
//显示新增类别页面
public void showCreatePage(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("WEB-INF/jsp/add_category.jsp").forward(request, response);
}
}
indexServlet:
package com.imooc.oa.controller;
import com.imooc.oa.entity.Book;
import com.imooc.oa.service.BookService;
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.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
@WebServlet("/index/*")
public class IndexServlet extends HttpServlet {
private BookService bookService=new BookService();
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
doGet(req, resp);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//HttpSession session=request.getSession();
List<Book> bookList = bookService.selectBook();
req.setAttribute("bookList", bookList);
req.getRequestDispatcher("/index.ftl").forward(req, resp);
// Book book=bookService.selectById(id);
// Category category=categoryService.selectById(book.getCategoryId());
// session.setAttribute("current_book",book);
// session.setAttribute("current_category",category);
// req.getRequestDispatcher("/WEB-INF/ftl/index.ftl").forward(req, resp);
}
}
book.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ 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/index.css">
<link rel="stylesheet" href="css/bootstrap.min.css">
</head>
<body>
<header>
<div class="container">
<nav>
<a href="#" >Java</a>
</nav>
<nav>
<a href="" >前端</a>
</nav>
<nav>
<a href="" >分类</a>
</nav>
</div>
</header>
<section class="banner">
<div class="container">
<div>
<h1>图书</h1>
<p>图书列表</p>
</div>
</div>
</section>
<section class="main">
<div class="container">
<table class="table table-striped">
<thead>
<tr>
<th>名称</th>
<th>分类</th>
<th>创建时间</th>
<th>最后修改时间</th>
</tr>
</thead>
<tbody>
<c:forEach items="${bookList}" var="book">
<tr>
<td>${book.name}</td>
<td>${book.categoryId}</td>
<td>${book.imgPath}</td>
<td><fmt:formatDate value="${book.createTime}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
<td><fmt:formatDate value="${book.updateTime}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</section>
<section class="page">
<div class="container">
<div id="fatie">
<a href="/management?method=bookCreate"><button>新建</button></a>
</div>
</div>
</section>
<footer>
copy@慕课网
</footer>
</body>
</html>
bookController:
package com.imooc.oa.controller;
import com.imooc.oa.entity.Book;
import com.imooc.oa.service.BookService;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
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.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
@WebServlet("/management/*")
public class BookController extends HttpServlet {
private BookService bookService=new BookService();
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, UnsupportedEncodingException {
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
doGet(req, resp);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String uri = req.getRequestURI();
String method = uri.substring(uri.lastIndexOf("/") + 1);
if (method.equals("bookCreate")) {
this.bookCreate(req, resp);
} else if (method.equals("bookAdd")) {
this.bookAdd(req, resp);
} else if (method.equals("showPartBook")) {
this.showPartBook(req, resp);
}
}
private void bookCreate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println(req.getContextPath());
req.getRequestDispatcher("/WEB-INF/jsp/add_book.jsp").forward(req, resp);
}
private void bookAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH");
//初始化FileUpload组件
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload sf = new ServletFileUpload(factory);
Book book = new Book();
try {
List<FileItem> formDate = sf.parseRequest(req);
for (FileItem fi : formDate) {
if (fi.isFormField()) {
switch (fi.getFieldName()) {
case "name":
book.setName(fi.getString("UTF-8"));
break;
case "categoryId":
book.setCategoryId(Integer.parseInt(fi.getString("UTF-8")));
break;
case "level":
book.setLevel(Integer.parseInt(fi.getString("UTF-8")));
break;
case "price":
book.setPrice(Integer.parseInt(fi.getString("UTF-8")));
break;
default:
break;
}
} else {
String path = req.getServletContext().getRealPath("/img");
System.out.println("文件上传目录:" + path);
File file = new File(path);
if (!file.exists()) {
file.mkdir();
}
String fileName = UUID.randomUUID().toString();
String substring = fi.getName().substring(fi.getName().lastIndexOf("."));
fi.write(new File(path, fileName + substring));
book.setImgPath(path + "/img/" + fileName + substring);
}
}
} catch (Exception e) {
e.printStackTrace();
}
HttpSession session = req.getSession();
book.setUpdateTime(new Date());
book.setCreateTime(new Date());
BookService bookService = new BookService();
bookService.createBook(book);
List<Book> bookList = bookService.selectBook();
session.setAttribute("bookList", bookList);
req.getRequestDispatcher("/WEB-INF/jsp/book.jsp").forward(req, resp);
}
private void showPartBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String categoryId = req.getParameter("categoryId");
List<Book> list = new ArrayList<Book>();
if (categoryId != null) {
list = bookService.selectById(Integer.parseInt(categoryId));
req.setAttribute("list", list);
req.getRequestDispatcher("/WEB-INF/jsp/book.jsp").forward(req, resp);
} else {
list = bookService.selectBook();
req.setAttribute("list", list);
req.getRequestDispatcher("/WEB-INF/jsp/book.jsp").forward(req, resp);
}
}
// HttpSession session = req.getSession();
// session.setAttribute("list", bookList);
// List<Book> list=new ArrayList<Book>();
// for(Book b:list){
// if(b.getCategoryId().equals(category.categoryId)){
// list.add(b);
//
// }
// }
// return (Book) list;
//req.setAttribute("bookList",list);
// req.getRequestDispatcher("/WEB-INF/jsp/book.jsp").forward(req, resp);
// }
}
index.ftl:
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
<!-- Custom Theme files -->
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="keywords" content="" />
<!-- //Custom Theme files -->
<link href="css/bootstrap.css" type="text/css" rel="stylesheet" media="all">
<link href="css/style.css" type="text/css" rel="stylesheet" media="all">
<!-- js -->
<script src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/bootstrap-3.1.1.min.js"></script>
<!-- //js -->
<!-- cart -->
<script src="js/simpleCart.min.js"> </script>
<!-- cart -->
<script type="text/javascript">
</script>
</head>
<body>
<!--header-->
<div class="header">
<div class="container">
<nav class="navbar navbar-default" role="navigation">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<h1 class="navbar-brand"><a href="">IMOOC</a></h1>
</div>
<!--navbar-header-->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li><a href="/index/list.do" class="active">首页</a></li>
</ul>
</div>
</nav>
</div>
<div class="clearfix"> </div>
</div>
</div>
<!--//header-->
<!--banner-->
<div class="banner">
<div class="container" style="height: 300px">
<h2 class="hdng">IMOOC <span>图书</span></h2>
<p>读万卷书,行万里路</p>
<a href="#">SHOP NOW</a>
<!--
<div class="banner-text">
<img src="../../../images/tushu.jpeg" alt=""/>
</div>
-->
</div>
</div>
<!--//banner-->
<!--gallery-->
<div class="gallery">
<div class="container">
<div class="gallery-grids">
<div class="col-md-3 gallery-grid " style="float:left">
<a href="#">
<img src="img\\Java.jpg" class="img-responsive" alt=""/>
<div class="gallery-info">
<p><span class="glyphicon glyphicon-eye-open" aria-hidden="true"></span> view</p>
<a class="shop" href="single.html">SHOP NOW</a>
<div class="clearfix"> </div>
</div>
</a>
<div class="galy-info">
<p style="size: 30px">Java基础</p>
<div class="galry">
<div class="prices">
<h5 class="item_price">¥50</h5>
</div>
<div class="rating">
<c:forEach begin="1" end="5" var="i">
<span>☆</span>
</c:forEach>
</div>
<div class="clearfix"></div>
</div>
</div>
</div>
</div>
<div align="right">
<!-- 分页部分可以参照MyBatis拦截器的例子-->
</div>
</div>
</div>
<!--//gallery-->
<!--subscribe-->
<!--//subscribe-->
<!--footer-->
<!--//footer-->
<div class="footer-bottom">
<div class="container">
<p>Copyright © 2017 imooc.com All Rights Reserved | 京ICP备 13046642号-2</p>
</div>
</div>
</body>
</html>
13
收起
正在回答 回答被采纳积分+1
1回答
java工程师2020版
- 参与学习 人
- 提交作业 9393 份
- 解答问题 16556 个
综合就业常年第一,编程排行常年霸榜,无需脱产即可学习,北上广深月薪过万 无论你是未就业的学生还是想转行的在职人员,不需要基础,只要你有梦想,想高薪
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星