为什么我在Spring提交表单后得到null对象?

时间:2017-11-04 12:07:15

标签: java spring spring-mvc spring-boot thymeleaf

我是Spring的新手,昨天我创建了一个简单的应用程序。我输入了书的标题,作者和流派,并将其保存到List<Book>。 Book.java包含私有字段(标题,作者,流派)。

因此创建书籍并将其保存到列表中可以正常工作。我也可以查看我添加的所有书籍。所以它工作正常。但现在我想创建并删除它们。所以我有BookService.java可以添加和删除书籍。

BookService.java

private List<Book> allBooks = new ArrayList<>();

public List<Book> getAllBooks() {
    return allBooks;
}

public void addBook(String title, String author, String genre) {
    allBooks.add(new Book(title, author, genre));
}

public void deleteBook(Book book) {
    allBooks.remove(book);
}

这是我的控制器中删除书籍的东西

@GetMapping("/books/delete")
public String deleteBook(Model model) {
    model.addAttribute("BookList", bookService.getAllBooks()); // Works fine
    return "delete";
}

@PostMapping("/books/delete")
public String deletedBook(@ModelAttribute Book book, Model model) {
    System.out.println(book.getTitle()); // null

    bookService.deleteBook(book); // can't delete null so nothing happens to the list
    model.addAttribute("deletedBook", book);
    return "deletedBookResult";
}

delete.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="UTF-8"/>
  <title>Title</title>
</head>
<body>

<h2>Delete page</h2>

<div th:each="bookObj : ${BookList}"> <!-- BookList - all books I add using submit form -->
  <form action="#" th:action="@{/books/delete}" th:object="${bookObj}" method="post"> <!-- I SEND bookObj -->
    <input type="submit" th:value="${bookObj.title}"/> <!-- WORKS. I GET BOOK'S NAME ON THIS BUTTON-->
  </form>
</div>

</body>
</html>

我使用 th:each =“bookObj:$ {BookList}”。所以bookObj是我添加的每本书。这就是我使用 th:object = $ {bookObj} 的原因。我后来添加的每本书都有一个表单。它显示按钮上的标题。但是当我按下它时,我对IDEA的控制台和网页都是空的。为什么? 提前谢谢。

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="UTF-8"/>
  <title>Title</title>
</head>
<body>

<h2 th:text=" 'You have just deleted' + ${deletedBook.title}"></h2>
<!-- You have just deleted null -->

</body>
</html>

1 个答案:

答案 0 :(得分:0)

您没有在表单中发送任何内容。添加一些隐藏字段:

<div th:each="bookObj : ${BookList}"> 
    <form action="#" th:action="@{/books/delete}" method="post">
        <input type="hidden" th:name="genre" th:value="${bookObj.genre}"/>
        <input type="hidden" th:name="author" th:value="${bookObj.author}"/>
        <input type="hidden" th:name="title" th:value="${bookObj.title}"/>
        <input type="submit" th:value="${bookObj.title}"/>
    </form>
</div>

或者,如果您不想在html中公开您的数据,您可以尝试使用某种会话对象(可能是一种过度杀伤,但有时可能会有用):

@GetMapping("/books/delete")
public String deleteBook(Model model, HttpSession session) {
    session.setAttribute("BookList", new Book[]{
            new Book("Title", "Tom","genre"),
            new Book("Title 2", "Jerry","genre2")}
    );
    return "delete";
}

@PostMapping("/books/delete")
public String deletedBook(HttpSession session, Integer id, Model model) {
    Book[] books = (Book[]) session.getAttribute("BookList");
    Book book = books[id];
    System.out.println(book); 
    model.addAttribute("deletedBook", book);
    return "deletedBookResult";
}

并使用它:

<div th:each="bookObj, iter : ${session.BookList}">
    <form action="#" th:action="@{/books/delete}" method="post">
        <input type="hidden" th:name="id" th:value="${iter.index}"/>
        <input type="submit" th:value="${bookObj.title}"/>
    </form>
</div>