简体   繁体   中英

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}"/> ?

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:

<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

public class User implements Serializable {

...

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

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

...

}

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

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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