[学习笔记] SpringMVC之SSM框架整合(二)

# 学习 # · 2021-02-24

实现查询Books

1、创建Books控制器(BooksController):

@Controller
@RequestMapping("/Books")
public class BooksController {
    // Controller层调用Service层
    @Autowired
    @Qualifier("BooksServiceImpl")
    private BooksService booksService;
}

2、在Controller中编写查询全部书籍的方法:

@RequestMapping("/list")
public String list(Model model) {
    List<Books> list = booksService.queryAllBook();
    model.addAttribute("list", list);
    return "list";
}

3、编写前端书籍列表页面(/WEB-INF/views/list.jsp):

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Books列表</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.bootcss.com/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>Books列表</h1>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-md-4 column">
            <a class="btn btn-primary" href="${pageContext.request.contextPath}/Books/add">新增</a>
        </div>
    </div>

    <div class="row clearfix">
        <div class="col-md-12 column">
            <table class="table table-hover table-striped">
                <thead>
                <tr><th>书籍编号</th><th>书籍名字</th><th>书籍数量</th><th>书籍详情</th><th>操作</th></tr>
                </thead>

                <tbody>
                <c:forEach var="book" items="${requestScope.get('list')}">
                    <tr>
                        <td>${book.getBookID()}</td>
                        <td>${book.getBookName()}</td>
                        <td>${book.getBookCounts()}</td>
                        <td>${book.getDetail()}</td>
                        <td>
                            <a href="${pageContext.request.contextPath}/Books/toUpdateBook?id=${book.getBookID()}">更改</a> |
                            <a href="${pageContext.request.contextPath}/Books/del/${book.getBookID()}">删除</a>
                        </td>
                    </tr>
                </c:forEach>
                </tbody>
            </table>
        </div>
    </div>
</div>
</body>
</html>

4、访问测试。


实现增加Books

1、在Controller中编写增加书籍的方法:

@RequestMapping("/add")
public String add() {
    return "add";
}

@RequestMapping("/addBook")
public String addBook(Books books) {
    //表单传入的参数必须和Books实体类属性一一对应
    booksService.addBook(books);
    return "redirect:/Books/list";    //重定向到/Books/list
}

2、编写前端增加书籍页面(/WEB-INF/views/add.jsp):

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ 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.bootcss.com/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>新增书籍</h1>
            </div>
        </div>
    </div>
    <form action="${pageContext.request.contextPath}/Books/addBook" method="post">
        书籍名称:<input type="text" name="bookName"><br><br><br>
        书籍数量:<input type="text" name="bookCounts"><br><br><br>
        书籍详情:<input type="text" name="detail"><br><br><br>
        <input type="submit" value="添加">
    </form>
</div>

3、访问测试。


实现修改Books

1、在Controller中编写修改书籍的方法:

@RequestMapping("/toUpdateBook")
public String toUpdateBook(Model model, int id) {
    Books books = booksService.queryBookById(id);
    model.addAttribute("book",books);
    return "edit";
}

@RequestMapping("/updateBook")
public String updateBook(Model model, Books book) {
    booksService.updateBook(book);
    Books books = booksService.queryBookById(book.getBookID());
    model.addAttribute("books", books);
    return "redirect:/Books/list";
}

2、编写前端修改书籍页面(/WEB-INF/views/edit.jsp):

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>修改信息</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- 引入 Bootstrap -->
    <link href="https://cdn.bootcss.com/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>修改信息</h1>
            </div>
        </div>
    </div>

    <form action="${pageContext.request.contextPath}/Books/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>

3、访问测试。


实现删除Books

1、在Controller中编写删除书籍的方法:

@RequestMapping("/del/{bookId}")
public String deleteBook(@PathVariable("bookId") int id) {
    booksService.deleteBookById(id);
    return "redirect:/Books/list";
}

2、访问测试。

如无特殊说明,本博所有文章均为博主原创。

如若转载,请注明出处:一木林多 - https://www.l5v.cn/archives/237/

评论