【问题标题】:How to access spring boot properties from freemarker template如何从 freemarker 模板访问 Spring Boot 属性
【发布时间】:2016-05-01 12:44:31
【问题描述】:

我的问题很简单:

在我的 spring-boot Web 应用程序中,我有一些前端/客户端需要了解的与 env 相关的属性(比如说,一个依赖于 env 的 CORS 远程 URL 调用)。

我已经正确定义了我的 application-{ENV}.properties 文件,并且所有 per-env-props 机制都工作正常。

我似乎找不到答案的问题是:你如何让你的 freemarker 上下文知道你的属性文件能够注入它们(特别是在 spring-boot 应用程序中)。这可能很容易,但我找不到任何例子......

谢谢,

【问题讨论】:

标签: java spring spring-boot freemarker


【解决方案1】:

我会回答自己:

spring-boot 1.3 中最简单的方法是覆盖 FreeMarkerConfiguration 类:

/**
 * Overrides the default spring-boot configuration to allow adding shared variables to the freemarker context
 */
@Configuration
public class FreemarkerConfiguration extends FreeMarkerAutoConfiguration.FreeMarkerWebConfiguration {

    @Value("${myProp}")
    private String myProp;

    @Override
    public FreeMarkerConfigurer freeMarkerConfigurer() {
        FreeMarkerConfigurer configurer = super.freeMarkerConfigurer();

        Map<String, Object> sharedVariables = new HashMap<>();
        sharedVariables.put("myProp", myProp);
        configurer.setFreemarkerVariables(sharedVariables);

        return configurer;
    }
}

【讨论】:

  • 这很有帮助,但我问自己是否有更好的解决方案可以自动将我的 application.properties 中的所有属性附加到我在此方法中手动执行的操作中
  • 如何在 .ftl 文件中获取此属性“myProp”?
  • @XingLee 你知道如何获得这个属性了吗?我也想知道哈哈哈
【解决方案2】:

spring boot 2 中的一个选项:

@Configuration
public class CustomFreeMarkerConfig implements BeanPostProcessor {

  @Value("${myProp}")
  private String myProp;

  @Override
  public Object postProcessBeforeInitialization(Object bean, String beanName)
  throws BeansException {
      if (bean instanceof FreeMarkerConfigurer) {
          FreeMarkerConfigurer configurer = (FreeMarkerConfigurer) bean;
          Map<String, Object> sharedVariables = new HashMap<>();
          sharedVariables.put("myProp", myProp);
          configurer.setFreemarkerVariables(sharedVariables);
      }
      return bean;
  }
}

Spring Boot 2.x 更改了类结构,因此不再可能像 Spring Boot 1.x 那样子类化并保持自动配置。

【讨论】:

    【解决方案3】:
    import freemarker.template.Configuration;
    
    @Component
    public class FreemarkerConfiguration {
    
        @Autowired
        private Configuration freemarkerConfig;
    
        @Value("${myProp}")
        private String myProp;
    
        Map<String, Object> model = new HashMap();
        model.put("myProperty", myProp);
      
    
        // set loading location to src/main/resources
        freemarkerConfig.setClassForTemplateLoading(this.getClass(), "/");
        Template template = freemarkerConfig.getTemplate("template.ftl");
        String templateText = FreeMarkerTemplateUtils.
                                        processTemplateIntoString(template, model);
    
    }   
    
    
         
      step 2, 
       get property in freemarker template code.
         <div>${myProperty}</td>
       
    

    【讨论】:

      猜你喜欢
      • 2013-02-24
      • 2021-07-27
      • 1970-01-01
      • 2012-05-22
      • 2015-12-30
      • 1970-01-01
      • 1970-01-01
      • 2014-07-31
      相关资源
      最近更新 更多