Ни BindingResult, ни простой целевой объект для имени компонента 'msg' не доступны в качестве атрибута запроса - PullRequest
0 голосов
/ 29 марта 2020

Я использую контактную форму для Spring Boot MVC и Thymeleaf. Я получаю эту ошибку и исключение при открытии /message:

ERROR 8766 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/message.html]")] with root cause

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'msg' available as request attribute

Мой контроллер:

 @GetMapping("/message")
 public String message() {
        return "message";
 }
 @PostMapping("/message")
 public String sendMessage(@ModelAttribute(name = "msg") Message msg, Model model, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return "main";
        }

        model.addAttribute("msg", msg);

        mailService.sendMessage(msg.getTitle(), msg.getMessage());

        return "message";
 }

сообщение. html:

<form action="/message" th:action="@{/message}" th:object="${msg}" method="post">
    <p>
        <label>Title:
            <input type="text" th:field="${msg.title}"/>
        </label>
    </p>
    <p>
        <label>Message:
            <input type="text" th:field="${msg.message}"/>
        </label>
    </p>
    <p>
        <input type="submit" value="Submit"/>
        <input type="reset" value="Reset"/>
    </p>
</form>

Что моя ошибка? На странице /message ошибка:

There was an unexpected error (type=Internal Server Error, status=500).
An error happened during template parsing (template: "class path resource [templates/message.html]")
org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/message.html]")
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...