官方文档地址:

2009年的地址
http://freemarker.sourceforge.net/docs/pgui_misc_beanwrapper.html#autoid_54

https://freemarker.apache.org/docs/pgui_misc_beanwrapper.html#autoid_60   更新于2018-05-23

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.

BeansWrapper wrapper = BeansWrapper.getDefaultInstance();
TemplateHashModel staticModels = wrapper.getStaticModels();
TemplateHashModel fileStatics =(TemplateHashModel) staticModels.get("java.io.File"); 

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:

root.put("File", fileStatics); 

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:

<#list File.listRoots() as fileSystemRoot>...</#list> 

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

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

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).

转载于:https://www.cnblogs.com/skey_chen/archive/2009/11/12/1601620.html

FreeMarker调用JAVA静态方法相关推荐

  1. Scala调用Java静态成员及静态方法注意点——只能通过当前类名调用而不能借助子类调用父类静态成员/方法

    只能通过当前类名NioUtil调用静态成员 package com.zxl.scalaimport cn.hutool.core.io.{FileUtil, IoUtil, NioUtil} impo ...

  2. android jni 调用java_Android JNI开发系列(九)JNI调用Java的静态方法实例方法

    JNI调用Java的静态方法&实例方法 package org.professor.jni.bean; import android.util.Log; /** * Created by pe ...

  3. 基于 Android NDK 的学习之旅----- C调用Java

    2019独角兽企业重金招聘Python工程师标准>>> 基于 Android NDK 的学习之旅----- C调用Java 许多成熟的C引擎要移植到Android 平台上使用 , 一 ...

  4. JNI基础 c语言调用java方法

    利用c语言调用java无参的方法 java方法 com.example.jniparsedata.ParseData类中的方法     //打印     public void sayHelloFro ...

  5. jni开发-java调用c函数,c调用java函数实现

    1.配置 下载ndk,cmake即可. 2.实现 MainActivity的实现 public class MainActivity extends AppCompatActivity {// Use ...

  6. 【Android NDK 开发】JNI 方法解析 ( C/C++ 调用 Java 方法 | 函数签名 | 调用对象方法 | 调用静态方法 )

    文章目录 I . 调用 Java 方法流程 II . 获取 jclass 对象 ( GetObjectClass ) III . 获取 jclass 对象 ( FindClass ) IV . JNI ...

  7. Java静态方法调用非静态方法

    静态方法(static mehod)不能直接调用非静态方法(non-static method),可以通过一个对象的引用传入静态方法中,再去调用该对象的非静态方法.在主函数(static方法)中,经常 ...

  8. Java中的静态方法为什么不能调用非静态方法

    hello,小伙伴们好,我是江湖人送外号[道格牙]的子牙老师. 昨晚京东大佬勇哥在群里分享了一道他新创的JVM面试题,我听完后觉得还挺有意思的,分享给大家 小佬们先别急着看我的分析,先自己想想答案 你 ...

  9. java静态方法的调用方法_Java静态方法和实例方法

    一:静态方法 其实在我们第一天学习java的时候就使用了静态方法,主方法的就是一个静态方法,静态方法和静态属性一样使用static修饰. DEMO:定义静态方法 发现了可以使用"类名.方法名 ...

最新文章

  1. IT顾问成长分享沙龙
  2. Docker容器的备份和还原
  3. Intent对象详解(二)
  4. python3精要(20)-函数(1)
  5. 社交大佬们的数据“大”在哪里?
  6. 二叉树的各种操作(转)
  7. windows复制文件到 vmware centos虚拟机问题
  8. C51单片机——指令系统
  9. 计算机系统应用的书,基于领域本体与上下文感知计算的智能图书-计算机系统应用.PDF...
  10. 吴裕雄 01-mysql管理
  11. 腾讯视频QLV格式转换为MP4格式
  12. hsf 架构_HSF源码剖析
  13. 2022-04-13 分布式任务调度Quartz
  14. 不可预料的压缩文件末端怎么解决
  15. [iOS]Size Class不同尺寸适配的是什么样的机型(实验向)
  16. Java模式设计卖电脑实验报告,面向对象(Java)实验0继承、接口和多态
  17. PHP中的SAPI是什么?如何实现?
  18. -XX:CMSInitiatingOccupancyFraction
  19. MainWindow.xaml.cs
  20. (转)腾讯微信引发的产品建设经验

热门文章

  1. web服务器time_wait值过高解决方案
  2. shell 实现memcache缓存命中率监控脚本
  3. 华为H3C ER3100由器限速方法合集
  4. 北京站售票人员倒票视频
  5. 纪念lxwcto(潜龙)
  6. 福师计算机导论在线作业一,福师《计算机导论》在线作业一..doc
  7. dicom文件_图像识别 | 使用Python对医学Dicom文件的预处理(含代码)
  8. Nginx — Overview
  9. 用 Flask 来写个轻博客 (6) — (M)VC_models 的关系(one to many)
  10. Node 中文编码 我为什么哭了