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

编程入门 行业动态 更新时间:2024-10-09 13:26:16
本文介绍了如何从Freemarker模板访问Java系统属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我开始使用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系统属性?

本文发布于:2023-11-28 17:48:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1643371.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   模板   系统   Freemarker   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!