2016-11-10 24 views
2

春季启动Maven的依赖Thymeleaf模板引擎不遵循表达式语言

<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", "[email protected]"); 
     String test=templateEngine.process(templateName, context); 
     return test; 
    } 
} 

这是我的邮件发送者的方法。

MimeMessage mimeMessage=javaMailSender.createMimeMessage(); 
//mimeMessage.setContent(mailContentBuilder.build("changepassword","alpha","ema [email protected]"), "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 protected]"), true); 
javaMailSender.send(mimeMessage); 

这是我的模板,在SRC /资源/模板

<!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}" 
+2

是一个HTML标签的属性,所以像

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

应该工作,从一眼判断'日:text'应该去上一个不在文本中某处的元素。 –

+1

我用它作为 helloooo th:text =“$ {user}”,相同的结果 –

+1

谢谢Deinum,我用它作为标签的属性。它现在工作。谢谢 –

回答