java - Thymeleaf 模板引擎不遵循表达式语言

标签 java spring spring-boot thymeleaf

Spring Boot Maven 依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

@Service
public class MailContentBuilder {

    private TemplateEngine templateEngine;

    @Autowired
    public MailContentBuilder(TemplateEngine templateEngine) {
        this.templateEngine=templateEngine;
    }

    public String build(String templateName,String user,String email) throws IOException {
        Context context=new Context();
        context.setVariable("user", "Alpha");
        context.setVariable("email", "alpha@gmail.com");
        String test=templateEngine.process(templateName, context);
        return test;
    }
}

这是我的邮件发件人方法。

MimeMessage mimeMessage=javaMailSender.createMimeMessage();
//mimeMessage.setContent(mailContentBuilder.build("changepassword","alpha","ema il@email.com"), "text/html");

MimeMessageHelper helper=new MimeMessageHelper(mimeMessage);
helper.setTo(auth0UserService.getUser(userid).getEmail());
helper.setFrom(fromUsername);
helper.setSubject("Password Change Confirmation");
helper.setText(mailContentBuilder.build("changepassword","alpha","email@email.com"), true);
javaMailSender.send(mimeMessage);

这是我的模板,在 src/resources/templates 中

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Change password</title>
    </head>
    <body >
        helloooo th:text="${user}"
    </body>
</html>

这是它发送的内容,它不遵循表达式语言,而是按原样写入页面。不使用变量。

helloooo th:text="${user}"

最佳答案

th:text 必须是 html 标签的属性,所以类似

<p th:text="helloooo ${user}" />

应该可以工作,看一眼 http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#using-texts 就可以判断

关于java - Thymeleaf 模板引擎不遵循表达式语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40527503/

相关文章:

maven - Spring 休息数据

java - 获取单词的五个连续组合

java - PayPal SDK 从付款审核页面到个人资料页面

java - Spring中基于自定义注解的方法调用?

java - 如何将 JDBI 添加到 Spring Boot 应用程序中?

Java多连接查询

java - 域文件夹在 Spring Boot 项目中起什么作用以及应该在其中存储哪些类?

java - Hazelcast sql 查询

java - 将子类对象分配给父类对象

java - 热点 JIT 优化