Freemarker 调用Java静态方法
2016-10-11 08:54
519 查看
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.
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:
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:
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
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).
Demo:
[java] view
plain copy
// 指定要在ftl页面使用的静态包名
public static TemplateHashModel useStaticPackage(String packageName)
{
try
{
BeansWrapper wrapper = BeansWrapper.getDefaultInstance();
TemplateHashModel staticModels = wrapper.getStaticModels();
TemplateHashModel fileStatics = (TemplateHashModel) staticModels.get(packageName);
return fileStatics;
}
catch (Exception e)
{
e.printStackTrace();
}
}
// 调用处
//data.put("list", list);
data.put("Helper",useStaticPackage("com.test.Helper"));
// ftl 页面
${Helper.method()}
The TemplateHashModel returned from BeansWrapper.getStaticModels() can be used to create hash models for accessing static methods and fields of an arbitrary class.
| |||
| |||
| |||
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
| |||
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).
Demo:
[java] view
plain copy
// 指定要在ftl页面使用的静态包名
public static TemplateHashModel useStaticPackage(String packageName)
{
try
{
BeansWrapper wrapper = BeansWrapper.getDefaultInstance();
TemplateHashModel staticModels = wrapper.getStaticModels();
TemplateHashModel fileStatics = (TemplateHashModel) staticModels.get(packageName);
return fileStatics;
}
catch (Exception e)
{
e.printStackTrace();
}
}
// 调用处
//data.put("list", list);
data.put("Helper",useStaticPackage("com.test.Helper"));
// ftl 页面
${Helper.method()}
相关文章推荐
- Struts2 FreeMarker FTL 模板文件中调用静态 java 方法
- Freemarker 调用Java静态方法
- FreeMarker调用JAVA静态方法
- Freemarker 调用Java静态方法
- freemarker调用java静态方法以及枚举
- Spring MVC使用FreeMarker调用java静态方法
- 今天发现c#中的实例后的对象是不能调用静态方法,以前学Java的时候是可以的.
- struts2 freemarker当中引进java 常量java静态方法
- 入门: 使用JNI 从C++代码中调用Java的静态方法
- java 调用静态方法和实例(对象)方法的区别
- cocos2d-x调用java中非静态方法以及静态方法注意事项以及示例
- java继承 父类子类方法调用问题 属性和静态方法是没有多态的
- struts调用java静态方法
- 今天发现c#中的实例后的对象是不能调用静态方法,以前学Java的时候是可以的.
- java 程序加载过程---3--类中申明同时申明类的静态对象 创建类的实例 访问类的静态变量 调用类的静态方法 使用反射方法 初始化类的子类对象 直接使用java.exe 调用某个类
- 关于Java中静态方法与非静态方法之间的互相调用问题
- Struts2 OGNL表达式中调用java静态方法无效的解决办法
- 分享一段JNI子线程中调用Java静态方法的代码
- Java 反射,调用类的静态方法
- java 非静态类调用静态方法的误区