【问题标题】:Thymeleaf Template Engine do not follow the expression languageThymeleaf 模板引擎不遵循表达式语言
【发布时间】:2016-11-10 12:21:07
【问题描述】:

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 应该放在不在文本某处的元素上。
  • 我用它作为 helloooo th:text="${user}",同样的结果
  • 谢谢 Deinum,我用它作为标签的属性。它现在起作用了。谢谢

标签: java spring spring-boot thymeleaf


【解决方案1】:

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

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

应该可以,从http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#using-texts的一瞥来看

【讨论】:

    猜你喜欢
    • 2021-03-16
    • 1970-01-01
    • 2015-12-31
    • 2020-08-27
    • 2015-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多