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

Dan Durnev

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

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>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why am I getting null values when submitting a form after editing some values with JS?

Why am i getting null object after i filled it?

after submitting edit form getting null value, when i am checking console

Why am I getting the following warning after submitting a form using Formik: A component is changing a controlled input to be uncontrolled

How Spring MVC getting data from Model and why is modelAttribute object created everytime after submitting form?

Why whenever I am submitting the form its asks for to select male/ female and the form is not getting submitted

Why am I getting [object Object] after accessing JSON data?

Why I am getting null

Why am I getting a null object from the database?

Why am I Getting a NullPointerException After Accessing an Object From SharedPreferences?

In my spring login form url getting changed after submitting the form

Why am I getting 'Uncaught TypeError: window[i] is not a function' when submitting authorize.net's AcceptUI form?

why it give me error when i am submitting a registration form

Why am getting null object error in this activity?

why am I getting this error windowed form?

Why am I getting getApplicationcontext() null?

Why am I getting a null pointer access?

Why am I getting null as a value of array?

Why am I NOT getting a Null Reference Exception?

Why am I getting null as the destination?

Why am I getting null in Console?

Why am I getting parameter value as null?

Why am i getting null for calculating a total?

Why am I getting object has no attribute?

Why am I getting an error in JSON object?

What is this Proxy Object and why am I getting it

Why am I getting a {"location": null} when I try to convert a text/xml into an object?

Why am I getting a null pointer exception for my object and how can I fix it?

Why am I getting an error for 'Column cannot be null' during my post api when I am passing a value from a html form