简体   繁体   English

如何从Freemarker模板访问Java系统属性?

[英]How to access Java system properties from Freemarker templates?

I started using Freemarker for assembling simple HTML pages, using FMPP Maven plugin . 我开始使用Freemarker来组装简单的HTML页面,使用FMPP Maven插件 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. 但我需要做的一件事是在页面上包含系统属性的值(Maven提供的系统属性之一)。 Is there a way to access system properties from Freemarker templates? 有没有办法从Freemarker模板访问系统属性? (if not, I may just have to hack plugin to allow passing values from Maven) (如果没有,我可能只需要破解插件以允许从Maven传递值)

cf https://community.jivesoftware.com/thread/14820 cf https://community.jivesoftware.com/thread/14820

You can access it like this : 您可以像这样访问它:

${statics['java.lang.System'].getProperty("my.property")}

cf documentation here : http://freemarker.sourceforge.net/docs/pgui_misc_beanwrapper.html cf document here: http//freemarker.sourceforge.net/docs/pgui_misc_beanwrapper.html

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. FMPP有一个名为data设置 ,它指定所有模板将看到的变量,因此您应该放置系统属性。 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. 因此,在这种情况下,您需要一个数据加载器,它将系统属性作为java.util.Properties对象返回。 While there's no data-loader specifically for that, you can use the eval data-loader like this (in your config.fmpp ): 虽然没有专门用于此的数据加载器,但您可以像这样使用eval数据加载器(在config.fmpp ):

data: {
   ...
   sysProps: eval('System.getProperties()')
   ...
}

Now in your templates you can access the system properties like sysProps["os.name"] . 现在,在模板中,您可以访问sysProps["os.name"]等系统属性。

Alternatively, you could write a custom FMPP data-loader. 或者,您可以编写自定义FMPP数据加载器。 See http://fmpp.sourceforge.net/dataloader.html#sect19 . 请参见http://fmpp.sourceforge.net/dataloader.html#sect19

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

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