0
私はSpringフォームを提出して遊んでいますが、今はオブジェクトに問題があります。それはnullです。どうしてか分かりません。私はこのオブジェクトをSystem.out.printlnすることに決めました、そして、私はすべてのデータを取得しますが、別のページに送信した後、それはnullです。この問題を解決するには?なぜ私はSpringにnullオブジェクトを投稿し、別のページに送信するのですか?
コントローラ
package com.megaproject3.MainProject.Controller;
import com.megaproject3.MainProject.Model.Book;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
@Controller
public class BookController
{
@GetMapping("/books/add")
public String addBook(Model model)
{
model.addAttribute("newAddedBook", new Book());
return "addNewBook";
}
@PostMapping("/books/view")
public String wwnewBook(@ModelAttribute Book book)
{
System.out.println(book.getAuthor() + " || " + book.getTitle()); // IT WORKS - I GET DATA I TYPE
return "result";
}
}
addNewBook.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>Add New Book</title>
</head>
<body>
<form action="#" th:action="@{/books/view}" th:object="${newAddedBook}" method="post">
<p>Title: <input type="text" th:field="*{title}"/></p>
<p>Author: <input type="text" th:field="*{author}"/></p>
<p>Genre: <input type="text" th:field="*{genre}"/></p>
<input type="submit" value="Submit"/> <input type="reset" value="Reset"/>
</form>
</body>
</html>
result.html
<!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 added ' + ${newAddedBook}"></h2> // You have just added null
<a href="/books/add">Add another one book</a>
</body>
</html>
はにありがとう前進。
おそらく、あなたの 'wwnewBook'投稿URLが' addBook'取得URLと異なるからでしょうか? – tsolakp