【问题标题】:Error while rendering HTML template using Thymeleaf 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor'使用 Thymeleaf 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' 渲染 HTML 模板时出错
【发布时间】:2019-11-27 14:52:37
【问题描述】:

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

错误是:org.thymeleaf.exceptions.TemplateProcessingException:执行处理器'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor'时出错(模板:“userPreview” - 第 10 行,第 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
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】:

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

    【讨论】:

      猜你喜欢
      • 2019-05-13
      • 2021-10-28
      • 2020-07-17
      • 2021-05-07
      • 2016-11-13
      • 1970-01-01
      • 1970-01-01
      • 2016-09-06
      • 2017-09-05
      相关资源
      最近更新 更多