SpringMVC实现简易线上图书馆(三)
接下来,继续实现修改书籍功能
1.BookController 类编写 , 方法三:修改书籍
//修改书籍
@RequestMapping("/toUpdateBook")
public String toUpdateBook(Model model,int id){
Books books = bookService.queryBookById(id);
System.out.println(books);
model.addAttribute("book",books);
return "updateBook";
}
@RequestMapping("/updateBook")
public String updateBook(Model model,Books book){
System.out.println(book);
bookService.updateBook(book);
Books books = bookService.queryBookById(book.getBookID());
model.addAttribute("books",books);
return "redirect:/book/allBook";
}
2.修改书籍页面 updateBook.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
Created by IntelliJ IDEA.
User: apple
Date: 2022/2/5
Time: 4:47 下午
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>修改信息</title>
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<link href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<div class="page-header">
<h1><small>修改信息</small></h1>
</div>
</div>
</div>
<form action="${pageContext.request.contextPath}/book/updateBook" method="post">
<input type="hidden" name="bookID" value="${book.getBookID()}">
书籍名称:<input type="text" name="bookName" value="${book.getBookName()}">
书籍数量: <input type="text" name="bookCounts" value="${book.getBookCounts()}">
书籍详情: <input type="text" name="detail" value="${book.getDetail()}">
<input type="submit" value="提交">
</form>
</div>
</body>
</html>
最后实现删除书籍的功能
@RequestMapping("/del/{bookId}")
public String deleteBook(@PathVariable("bookId") int id){
bookService.deleteBookByID(id);
return "redirect:/book/allBook";
}
再次描述一下实现流程:
用户通过前端提交相关的功能指令,前端跳到Controller层,Controller层调用Service层,Service层调用Dao层中实现的方法,Dao层的方法对数据库中的内容进行操作。