SpringMVC实现简易线上图书馆(二)

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页面
以上就是实现流程,其他功能和这个类似,非常重要!!!

如果对您有帮助,免费的赞点一个~~~感谢🙏

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值