Dan Durnev
Dan Durnev

Reputation: 89

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

I am new to Spring and yesterday I created a simple app. I type book's title, author and genre and it saves it to List<Book>. Book.java contains private fields (title, author, genre).

So creating books and saving them to list works fine. Also I can view all books I have added. So it works fine. But now I want to create and delete them. So I have BookService.java that can add and delete books.

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);
}

This is stuff in my controller to delete books

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

I use th:each="bookObj : ${BookList}". So bookObj is every book I add. That's why I use th:object=${bookObj}. There is a form for each book I added later. And it display's it's title on the button. But when I press it, I get null to IDEA's console and on webpage. Why? Thank you in advance.

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

Upvotes: 1

Views: 2718

Answers (1)

varren
varren

Reputation: 14731

You are not sending anything in your form. Add some hidden fields:

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

Or if you don't want to expose your data in html, you can try to use some sort of session objects instead (Probably an overkill, but sometimes can be useful):

@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";
}

And use it like:

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

Upvotes: 2

Related Questions