Java Thymeleaf模板引擎不遵循表达式语言
Spring引导Maven依赖项Java Thymeleaf模板引擎不遵循表达式语言,java,spring,spring-boot,thymeleaf,Java,Spring,Spring Boot,Thymeleaf,Spring引导Maven依赖项 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> @Service public class MailContentBuilder { private Templa
<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;
}
}
这是我的模板,在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>
文本必须是html标记的属性,所以
<p th:text="helloooo ${user}" />
应该可以工作,从看一眼判断,th:text应该放在文本中某个地方以外的元素上。我将它用作hellooo th:text=“${user}”,相同的结果谢谢你,我将它用作标记的属性。它现在起作用了。非常感谢。
helloooo th:text="${user}"
<p th:text="helloooo ${user}" />