如何将对象传递到百里香模板并访问其属性?

时间:2018-10-10 08:53:16

标签: java templates thymeleaf

是否可以通过上下文myContext.setVariable("user", myUser)将对象(假设用户包含3个String属性-名称,密码,详细信息)传递给百里香模板,并从这样的模板<div th:text="${user.name}"/>访问它的属性?< / p>

如果是的话,我该怎么做?

我的对象包含很多属性,我试图避免创建带有很多变量的上下文。

我对百里香很陌生,谢谢您的帮助。

1 个答案:

答案 0 :(得分:1)

如果您使用弹簧和百里香叶,那么它们应该像魅力一样为您工作。在这种情况下,它很简单:

    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,其内容如下:

<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配置:

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版本:

<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>