2016-11-10 26 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,我用它作爲標籤的屬性。它現在工作。謝謝 –

回答