java - 使用 Thymeleaf 渲染 HTML 模板时出错 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor'

标签 java spring-boot thymeleaf

我在使用 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 :

@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;
    }
}

最佳答案

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

关于java - 使用 Thymeleaf 渲染 HTML 模板时出错 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59072860/

相关文章:

java - 使用 Spring Boot 在微服务中进行 JWT 授权

unit-testing - 使用 Thymeleaf 模板引擎对服务进行单元测试时出现问题

java - Struts 2 与 Thymeleaf

spring - 将 Jdbc session 添加到 Spring Boot

java - 在 Controller 中处理删除请求的正确方法是什么?

mysql - 带有 Thymeleaf、Spring boot、Mysql 的 Ckeditor

java - 如何编辑具有预定义值的数组以包含用户输入 JAVA

javascript - 通过 AJAX 访问文件的正确 url 是什么?

java - 连接 Datomic 数据库时出错

java - 在 Spring Data JPA 和 Spring Boot 应用程序中启用全局 Hibernate 过滤器