ssmbiuld整合-增删改功能








controller:

package com.kakafa.controller;

import com.kakafa.pojo.Books;
import com.kakafa.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/book")
public class BookController {

    @Autowired  //在属性中使用 @Autowired 注解来除去 setter 方法
    @Qualifier("BookServiceImpl")
    private BookService bookService;

    //查询所有书籍
    @RequestMapping("/allbook")
    public String list(Model model){
        List<Books> books=bookService.queryAllBook();
        model.addAttribute("list",books);
        return "allBook";
    }

    //跳转到增加书籍的页面
    @RequestMapping("/toaddbook")
    public String toaddBook(){
        return "addBook";
    }

    //添加书籍的请求
    @RequestMapping("/addbook")
    public String addBook(Books books){
        bookService.addBook(books);
        return "redirect:/book/allbook";
    }

    //修改
    @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);
        return "redirect:/book/allbook";
    }

    //删除书籍
    @RequestMapping("/del/{bookId}")
    public String deleteBook(@PathVariable("bookId") int id) {
        bookService.deleteBookById(id);
        return "redirect:/book/allbook";
    }

}

allBook.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>
</head>
<body>
<h1>书籍展示</h1>

<a href="${pageContext.request.contextPath}/book/toaddbook">新增书籍</a>

<table>
    <tr>
        <th>书籍编号</th>
        <th>书籍数量</th>
        <th>书籍名称</th>
        <th>书籍详情</th>
        <th>操作</th>
    </tr>

       <c:forEach var="book" items="${list}">
            <tr>
                <td>${book.bookID}</td>
                <td>${book.bookCounts}</td>
                <td>${book.bookName}</td>
                <td>${book.detail}</td>
                <td>
                    <a href="${pageContext.request.contextPath}/book/toUpdateBook?id=${book.getBookID()}"> 修改 </a>
                    <a href="${pageContext.request.contextPath}/book/del/${book.getBookID()}">删除</a>
                </td>
            </tr>
       </c:forEach>

</table>
</body>
</html>


updateBook.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>修改信息</title>
</head>
<body>


<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>

</body>
</html>


addBook.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>新增书籍页面</title>
</head>
<body>

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




</body>
</html>



kuang老师的代码:https://mp.weixin.qq.com/s/SDxqGu_il3MUCTcN1EYrng


注意:过程中出现中文乱码的问题,发现是因为springMvc的乱码过滤写错了,正确的是如下:

    <!--springmvc过滤器,防止乱码-->
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
posted @ 2022-01-18 17:29  卡卡发  阅读(42)  评论(0)    收藏  举报