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