如何将对象传递给 thymeleaf 模板并访问其属性? [英] How can I pass object to thymeleaf template and access its attributes?
问题描述
是否可以通过上下文myContext.setVariable("user", myUser)
将对象让我们说用户(包含 3 个字符串属性 - 名称、密码、详细信息)传递给 thymeleaf 模板并访问它像这样的模板属性 <div th:text="${user.name}"/>
?
Is it possible to pass object let's say User (who contains 3 String attributes - name, password, detail) to thymeleaf template via context myContext.setVariable("user", myUser)
and access it attributes from template like this <div th:text="${user.name}"/>
?
如果是这样,我该怎么做?
If so how can I do that ?
我的对象包含很多属性,我试图避免使用大量变量创建上下文.
My object contains a lot of attributes and I am trying to avoid creating context with a lot of variables.
我对 thymeleaf 很陌生,所以感谢您的帮助.
I am very new to thymeleaf so thank you for any help.
推荐答案
如果您使用的是 spring 和 thymeleaf,那么它们应该对您有用.在这种情况下,它很简单:
If you are using spring and thymeleaf then they should work for you like a charm. In the case it's as simple as:
private static final VAR_USER = "user"
@Autowired
private SpringTemplateEngine templateEngine;
...
public void method(User user,...) {
Map<String, Object> variables;
variables.put(VAR_USER, user);
context.setVariables(variables);
org.thymeleaf.context.Context context = new Context(locale);
String evaluated = templateEngine.process("myTemplate", context);
}
其中 myTemplate 指的是 resources/mails/myTemplate.html
,其内容如下所示:
where myTemplate refers to resources/mails/myTemplate.html
and it's content looks like:
<p th:text="#{email.userActivity.greeting}">Hello</p>
<p th:text="#{email.userActivity.text1}">Following user activity...</p>
<ul>
...
<li th:text="#{email.userActivity.phone(${user.phoneNumber}?: #{error.not.provided})}">Phone number:</li>
<li th:text="#{email.userActivity.membershipNumber(${user.membershipNumber}?: #{error.not.provided})}">Membership number:</li>
...
</ul>
<p th:text="#{email.userActivity.text2}">Thanks for taking care of this demand within the agreed period!</p>
<p th:text="#{email.userActivity.text3}">Regards</p>
和我的 User
实体
public class User implements Serializable {
...
@Column(name = "membership_number")
private String membershipNumber;
@Column(name = "phone_number")
private String phoneNumber;
...
}
然后,我的 Thymeleaf 配置:
Then, my Thymeleaf configuration:
package my.package.config;
import my.package.MyTemplateEngine;
import org.apache.commons.lang3.CharEncoding;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Description;
import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
import org.thymeleaf.templateresolver.ITemplateResolver;
import org.thymeleaf.templateresolver.StringTemplateResolver;
@Configuration
public class ThymeleafConfiguration {
private MyTemplateEngine templateEngine;
@Bean
@Description("Thymeleaf template resolver serving HTML 5 emails from template file")
public ITemplateResolver htmlTemplateResolver() {
ClassLoaderTemplateResolver emailTemplateResolver = new ClassLoaderTemplateResolver();
emailTemplateResolver.setPrefix("mails/");
emailTemplateResolver.setSuffix(".html");
emailTemplateResolver.setTemplateMode(TemplateMode.HTML);
emailTemplateResolver.setCharacterEncoding(CharEncoding.UTF_8);
emailTemplateResolver.setCheckExistence(true);
return emailTemplateResolver;
}
@Description("Thymeleaf template resolver serving HTML 5 emails from input string")
@Bean
public ITemplateResolver stringTemplateResolver() {
final StringTemplateResolver templateResolver = new StringTemplateResolver();
templateResolver.setTemplateMode(TemplateMode.HTML);
return templateResolver;
}
@Bean
public MyTemplateEngine createTemplateEngine() {
templateEngine = new MyTemplateEngine();
return templateEngine;
}
}
以及我使用的 Thymeleaf 版本:
and the version of Thymeleaf that I use:
<properties>
...
<thymeleaf-layout-dialect.version>2.2.1</thymeleaf-layout-dialect.version>
<thymeleaf.version>3.0.6.RELEASE</thymeleaf.version>
...
</properties>
<dependencies>
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring4</artifactId>
<version>3.0.6.RELEASE</version>
</dependency>
...
</dependencies>
这篇关于如何将对象传递给 thymeleaf 模板并访问其属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!