SpringMVC实现简易线上图书馆(二)
继续实现增加书籍的功能
1.在controller层添加方法
//跳转到增加书籍页面
@RequestMapping("toAddPaper")
public String toAddPaper(){
return "addBook";
}
//添加书籍的请求
@RequestMapping("addBook")
public String addBook(Books books){
System.out.println("addBook=>"+books);
bookService.addBook(books);
return "redirect:/book/allBook";//重定向到我们到@RequestMapping("/allBook")请求;
}
编写addBook.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/addBook" method="post">
<div class="form-group">
<label >书籍名称:</label>
<input type="text" name="bookName" class="form-control" required >
</div>
<div class="form-group">
<label >书籍数量:</label>
<input type="text" name="bookCounts" class="form-control" required>
</div>
<div class="form-group">
<label >书籍评价:</label>
<input type="text" name="detail" class="form-control" required>
</div>
<div class="form-group">
<input type="submit" class="form-control" value="添加">
</div>
</form>
</div>
</body>
</html>
实现思路
前端新增一个超链接(新增),点击新增后,回跳转到Controller层的toAddPaper
这里直接跳转到addBook.jsp页面
在这个页面输入书籍的相关信息,点击提交
通过form表单提交相关信息,传输到Controller层的addBook
此时得到了传输过来的book对象,调用service层中的addBook方法,添加书籍,Service层进而调用dao层实现添加书籍的方法。最后通过重定向的方式回到allBook页面
以上就是实现流程,其他功能和这个类似,非常重要!!!