简体   繁体   English

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

[英]How can I pass object to thymeleaf template and access its attributes?

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}"/> ? 是否可以通过上下文myContext.setVariable("user", myUser)将对象(假设用户包含3个字符串属性-名称,密码,详细信息)传递给百里香模板myContext.setVariable("user", myUser)并从模板中访问它的属性,例如<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. 我的对象包含很多属性,并且我试图避免创建带有很多变量的上下文。

I am very new to thymeleaf so thank you for any help. 我对百里香叶很陌生,因此感谢您的帮助。

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);
    }

where myTemplate refers to resources/mails/myTemplate.html and it's content looks like: 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>

and my User entity 和我的User实体

public class User implements Serializable {

...

    @Column(name = "membership_number")
    private String membershipNumber;

    @Column(name = "phone_number")
    private String phoneNumber;

...

}

Then, my Thymeleaf configuration: 然后,我的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;
    }
}

and the version of Thymeleaf that I use: 以及我使用的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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM