Pie*_*let 6 java spring freemarker spring-boot
我的问题非常简单:
在我的spring-boot Web应用程序中,我有一些与env相关的属性,前端/客户端需要知道(比方说,一个CORS远程url来调用它依赖于env).
我已正确定义了我的应用程序 - {ENV} .properties文件,并且所有per-env-props mecanism都正常工作.
我似乎无法找到答案的问题是:如何让你的freemarker上下文知道你的属性文件能够注入它们(特别是在spring-boot应用程序中).这可能很容易,但我找不到任何例子......
谢谢,
我自己来回答:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3373 次 |
| 最近记录: |