我正在尝试输出已由百里香叶处理的html,因此将填写$ {name},$ {address}等值的值。
但实际上发生的事情是,templateEngine正在拆分html,它实际上与它进行的实际相同。
主要关注来自memorynotfound.com的tutorial
这是我的 ThymeleafConfig
@Configuration
public class ThymeleafConfig {
@Bean
public SpringTemplateEngine springTemplateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.addTemplateResolver(htmlTemplateResolver());
return templateEngine;
}
@Bean
public SpringResourceTemplateResolver htmlTemplateResolver(){
SpringResourceTemplateResolver emailTemplateResolver = new SpringResourceTemplateResolver();
emailTemplateResolver.setPrefix("classpath:/templates/");
emailTemplateResolver.setSuffix(".html");
emailTemplateResolver.setTemplateMode(StandardTemplateModeHandlers.HTML5.getTemplateModeName());
emailTemplateResolver.setCharacterEncoding(StandardCharsets.UTF_8.name());
emailTemplateResolver.setCacheable(false);
return emailTemplateResolver;
}
}
我的方法我正在调用templateEngine
@Component
public class ThymeleafEngine {
@Autowired
private SpringTemplateEngine templateEngine;
Email email = new Email();
public String process () {
Map model = new HashMap();
model.put("name", "Bob");
email.setMap(model);
Context context = new Context();
context.setVariables(email.getMap());
templateEngine.initialize();
String html = templateEngine.process("email_template", context);
return html;
}
}
电子邮件对象
public class Email {
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
Map map;
public Map getMap() {
return map;
}
public void setMap(Map map) {
this.map = map;
}
}
我在这篇文章中不再适合代码,但html非常基本,唯一区别在于<p>
标记内的$ {name}。
答案 0 :(得分:1)
<p th:text="'Dear ' + ${name} + ','" />
需要在html模板中 NOT
<p>Dear ${name}, </p>