使用Thymeleaf'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor呈现HTML模板时出错,

问题描述 投票:0回答:1

我在使用Thymeleaf和Spring Boot呈现HTML页面时遇到麻烦。尝试将html文件中的字段标记为类中的字段时出现错误。

错误是:org.thymeleaf.exceptions.TemplateProcessingException:执行处理器'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor'时出错(模板:“ userPreview”-第10行,第32列,第32行)]

HTML模板:

 <!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Email User Preview</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1>Send E-mail:</h1>
<form action="#" th:action="@{/sendmail}" th:object="${message}" method="post">
    <p>To:: <input type="text" th:field="*{receiverEmail}" /></p>
    <p>Subject: <input type="text" th:field="*{subject}" /></p>
    <p>Message: <input type="text" th:field="*{message}" /></p>
    <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
</form>
</body>
</html>

Controller:

@Controller
public class TestController {

    @GetMapping("/test")
    public String send() {
        user.setEmailAddress("yasseen.salama@gmail.com");
        try {
            emailService.sendMail(user, "Hello", "Test");

        } catch (MailException mailException) {
            System.out.println(mailException);
        }
        return "Email sent.";
    }
    @GetMapping("/sendmail")
    public String sendingMail(Model model) {
        Message message = new Message();
        model.addAttribute("userPreview", message);
        return "userPreview";
    }

    @PostMapping("/sendmail")
    public String mailSubmit(@ModelAttribute Message message) {
        return "Result";
    }
}

课程消息:

public class Message {
    String receiverEmail;
    String subject;
    String message;

    public String getReceiverEmail() {
        return receiverEmail;
    }

    public void setReceiverEmail(String receiverEmail) {
        this.receiverEmail = receiverEmail;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}
java spring-boot thymeleaf
1个回答
1
投票

要在模板中使用的对象或变量名称是userPreview,而不是message,因为这是您的Model对象中的键

热门问题
推荐问题
最新问题