当前位置: 首页 > 知识库问答 >
问题:

如何将对象传递给thymeleaf模板并访问其属性?

宇文智敏
2023-03-14

是否可以通过contextmycontext.setvariable(“User”,myUser)将对象(比方说User(包含3个字符串属性-name、password、detail)传递给thymeleaf模板,并从模板中访问它的属性,例如

如果是这样,我该怎么做?


匿名用户

如果你正在使用spring和thymeleaf,那么它们应该像魅力一样为你工作。在这种情况下,它非常简单:

    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>

和我的用户实体

public class User implements Serializable {

...

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

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

...

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

和我使用的沉香酒的版本:

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

共有2个答案

钮巴英
2023-03-14
相关问题
裴育
2023-03-14

如果你正在使用spring和thymeleaf,那么它们应该像魅力一样为你工作。在这种情况下,它非常简单:

    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>

和我的用户实体

public class User implements Serializable {

...

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

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

...

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

和我使用的沉香酒的版本:

<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>
 类似资料:
  • 问题内容: 我的node.js项目有一个Jade模板。我想将一个对象发送到jade模板,并将其传递给页面内的一个函数(以呈现某些内容)。 我确定我是从服务器这样发送正确的东西 在客户端中,我这样做: 因此,在脚本函数内部,我想调用一个函数,该函数使用在服务器端创建的json创建可视化。 问题是渲染时我有类似的东西。我还尝试发送JSON的字符串化版本,但是当我这样做时会抱怨。 我发送的json始终是

  • 问题内容: 尝试访问传递给模板的函数时出现错误: 有人可以让我知道我在做什么错吗? 模板文件(struct.tpl): 调用文件: 这是用于生成struct样板代码的程序(以防万一有人想知道为什么我要在模板中这样做)。 问题答案: 自定义函数需要在解析模板之前进行注册,否则解析器将无法分辨标识符是否为有效的函数名。模板被设计为可静态分析的,这是必需的。 您可以先使用创建一个新的未定义模板,并且除了

  • 我在示例中看到如何将消息字符串传递给亚马逊 sns sdk 的发布方法。但是,是否有如何将自定义对象作为消息传递的示例?我尝试将“消息结构”设置为“json”,但随后我得到错误。我应该将对象值传递到参数中的哪个位置? 有什么例子吗?

  • 我知道接受参数和,所以我已经尝试了2次(失败)来包装函数和args...: a.创建一个C样式的函数,该函数将调用传递给它的对象: 这会在GCC 4.8.5中导致以下错误: /usr/include/c++/4.8.2/functional:在'struct std::_bind_simple(std::_placeholder<1>,int))(int*)>>()>'的实例化中:/usr/inc

  • 如果对象为NULL,则无法传递对象项目。如何将对象从HTML传递给spring控制器?

  • 如何从 login.microsoftonline.com 获取AD访问令牌并将访问令牌传递给Web api控制器?因为我需要“访问令牌”才能通过 post 请求传递到另一个合作伙伴公司网站 URL。 下面的代码按照预期进行AAD认证,但我也需要“访问令牌”,