freemarker 调用 java方法_FreeMarker调用JAVA静态方法

"这篇博客介绍了如何使用模板引擎(如Freemarker)访问任意类的静态方法和字段。通过BeansWrapper实例获取静态模型,可以创建一个模板哈希模型,允许在模板中直接调用如`${File.SEPARATOR}
摘要由CSDN通过智能技术生成

官方文档地址:

Accessing static methods

The TemplateHashModel returned from BeansWrapper.getStaticModels() can be used to create hash models for accessing static methods and fields of an arbitrary class.

a77bfeddf5d548780289b04a0100562d.gif

a77bfeddf5d548780289b04a0100562d.gif

a77bfeddf5d548780289b04a0100562d.gif

a77bfeddf5d548780289b04a0100562d.gif

BeansWrapper wrapper = BeansWrapper.getDefaultInstance();

TemplateHashModel staticModels = wrapper.getStaticModels();

TemplateHashModel fileStatics =

(TemplateHashModel) staticModels.get("java.io.File");

a77bfeddf5d548780289b04a0100562d.gif

a77bfeddf5d548780289b04a0100562d.gif

a77bfeddf5d548780289b04a0100562d.gif

a77bfeddf5d548780289b04a0100562d.gif

And you will get a template hash model that exposes all static methods and static fields (both final and non-final) of the java.lang.System class as hash keys. Suppose that you put the previous model in your root model:

a77bfeddf5d548780289b04a0100562d.gif

a77bfeddf5d548780289b04a0100562d.gif

a77bfeddf5d548780289b04a0100562d.gif

a77bfeddf5d548780289b04a0100562d.gif

root.put("File", fileStatics);

a77bfeddf5d548780289b04a0100562d.gif

a77bfeddf5d548780289b04a0100562d.gif

a77bfeddf5d548780289b04a0100562d.gif

a77bfeddf5d548780289b04a0100562d.gif

From now on, you can use ${File.SEPARATOR} to insert the file separator character into your template, or you can even list all roots of your file system by:

a77bfeddf5d548780289b04a0100562d.gif

a77bfeddf5d548780289b04a0100562d.gif

a77bfeddf5d548780289b04a0100562d.gif

a77bfeddf5d548780289b04a0100562d.gif

...#list>

a77bfeddf5d548780289b04a0100562d.gif

a77bfeddf5d548780289b04a0100562d.gif

a77bfeddf5d548780289b04a0100562d.gif

a77bfeddf5d548780289b04a0100562d.gif

Of course, you must be aware of the potential security issues this model brings.

You can even give the template authors complete freedom over which classes' static methods they use by placing the static models hash into your template root model with

a77bfeddf5d548780289b04a0100562d.gif

a77bfeddf5d548780289b04a0100562d.gif

a77bfeddf5d548780289b04a0100562d.gif

a77bfeddf5d548780289b04a0100562d.gif

root.put("statics", BeansWrapper.getDefaultInstance().getStaticModels());

a77bfeddf5d548780289b04a0100562d.gif

a77bfeddf5d548780289b04a0100562d.gif

a77bfeddf5d548780289b04a0100562d.gif

a77bfeddf5d548780289b04a0100562d.gif

This object exposes just about any class' static methods if it's used as a hash with class name as the key. You can then use expression like ${statics["java.lang.System"].currentTimeMillis()} in your template. Note, however that this has even more security implications, as someone could even invoke System.exit() using this model if the method exposure level is weakened to EXPOSE_ALL.

Note that in above examples, we always use the default BeansWrapper instance. This is a convenient static wrapper instance that you can use in most cases. You are also free to create your own BeansWrapper instances and use them instead especially when you want to modify some of its characteristics (like model caching, security level, or the null model representation).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值