我开始使用Freemarker组装简单的HTML页面,使用 FMPP Maven插件。到现在为止还挺好。 但我需要做的一件事是在页面上包含系统属性的值(Maven提供的系统属性之一)。有没有办法从Freemarker模板访问系统属性? (如果不是,我可能只需要破解插件以允许从Maven传递值)
I started using Freemarker for assembling simple HTML pages, using FMPP Maven plugin. So far so good. But one thing I need to do is to include value of a system property (one of system properties Maven provides) on a page. Is there a way to access system properties from Freemarker templates? (if not, I may just have to hack plugin to allow passing values from Maven)
推荐答案FMPP有一个设置名为 data ,指定所有模板都会看到的变量,因此您应该放置系统属性。要将值放入其中,除非可以将值指定为简单文字,否则您需要一个所谓的数据加载器。因此,在这种情况下,您需要一个数据加载器,它将系统属性作为 java.util.Properties 对象返回。虽然没有专门用于此的数据加载器,但您可以像这样使用 eval 数据加载器(在 config.fmpp ):
FMPP has a setting called data that specifies the variables that all templates will see, so that's where you should put the system properties. To put values into there, unless the value can be specified as a simple literal, you need a so called data-loader. So in this case you need a data-loader that returns the system properties as a java.util.Properties object. While there's no data-loader specifically for that, you can use the eval data-loader like this (in your config.fmpp):
data: { ... sysProps: eval('System.getProperties()') ... }现在在你的模板中可以访问系统属性,如 sysProps [os.name] 。
Now in your templates you can access the system properties like sysProps["os.name"].
或者,您可以编写自定义FMPP数据加载器。请参见 fmpp.sourceforge/dataloader.html#sect19 。
Alternatively, you could write a custom FMPP data-loader. See fmpp.sourceforge/dataloader.html#sect19.
更多推荐
如何从Freemarker模板访问Java系统属性?
发布评论