【问题标题】:Why am I getting null object after submitting form in Spring?为什么我在 Spring 中提交表单后得到空对象?
【发布时间】:2018-04-17 01:23:48
【问题描述】:

我是 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";
}

删除.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>

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 在隐藏字段中公开大量私人日期将是无稽之谈。所以我认为会话方法并不过分,这是必要的。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多