简体   繁体   English

使自定义TemplateMethodModels在Freemarker中可用于Spring Boot 2

[英]Make custom TemplateMethodModels available in Freemarker for Spring Boot 2

I created several implementations of freemarker.template.TemplateMethodModelEx for my Freemarker templates in my Spring Boot 2 project. 我在Spring Boot 2项目中为Freemarker模板创建了freemarker.template.TemplateMethodModelEx几种实现。 Currently, in order to use them in my views, I need to add them to the model (“hash” in Freemarker terms) in every WebMVC controller. 当前,为了在我的视图中使用它们,我需要将它们添加到每个WebMVC控制器的模型中(用Freemarker术语表示为“哈希”)。

I'd like to have a way to make them available in every view of my project without having to extend the model in every controller. 我希望有一种方法可以使它们在项目的每个视图中可用,而不必在每个控制器中扩展模型。

FreeMarker has a feature to add top-level variables on the level of the Configuration singleton, via the Configuration.setSharedVariable(String, Object) API. FreeMarker具有通过Configuration.setSharedVariable(String, Object) API在Configuration单例级别上添加顶级变量的功能。 Apparently, Spring FreeMarkerConfigurationFactory exposes that under the property name "freemarkerVariables": https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/ui/freemarker/FreeMarkerConfigurationFactory.html#setFreemarkerVariables-java.util.Map- 显然,Spring FreeMarkerConfigurationFactory在属性名称“ freemarkerVariables”下公开了它: https : //docs.spring.io/spring/docs/current/javadoc-api/org/springframework/ui/freemarker/FreeMarkerConfigurationFactory.html#setFreemarkerVariables-java。 util.Map-

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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