繁体   English   中英

在Spring提交表单后,为什么会得到空对象?

[英]Why am I getting null object after submitting form in Spring?

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

您没有发送任何形式的表格。 添加一些隐藏的字段:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM