如何防止在模板中使用环境变量?

How to prevent of using environment variables with templates?

我有一个带有 Mustache 模板的 Spring Boot (2.1.4.RELEASE) Web 应用程序。一切正常,但我提到了奇怪的行为:自定义名称为“user.name”的占位符的值与我预期的不同。看起来 Mustache 从 系统变量 .

中获取了它
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mustache</artifactId>
        </dependency>

我将域对象用户从控制器传递给模板。用户有 "name" 属性.

@Controller
@RequestMapping("/")
class MyController {
    @GetMapping
    ModelAndView get() {
        return new ModelAndView(
                    "my_view"
            Collections.singletonMap("user", new User())
        );
    }
}

这是我的小胡子配置:

@Component
public class MustacheConfig {
    @Bean
    public Mustache.Compiler mustacheCompiler(
        Mustache.TemplateLoader mustacheTemplateLoader,
        Environment environment
    ) {

        MustacheEnvironmentCollector collector = new MustacheEnvironmentCollector();

        collector.setEnvironment(environment);

        return Mustache.compiler()
            .defaultValue("")
            .withLoader(mustacheTemplateLoader)
            .withCollector(collector);

    }
}

我尝试对 "environment" 对象进行一些操作,但它是不可变的。

作为一个选项,我可以将 "user.name" 名称更改为类似 "blablaUser.name" 的名称,但我认为这不是正确的方法。

解决方法是使用这个指针:

{this.user.name}

它明确指出变量的上下文。