FreeMarker calls JAVA static method

Source: Internet
Author: User
Tags file separator
Official Document address:
Http://freemarker.sourceforge.net/docs/pgui_misc_beanwrapper.html#autoid_54 Accessing static methods

TheTemplateHashModelReturned fromBeansWrapper. 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) ofJava. lang. SystemClass 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:

<#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

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 cocould even invokeSystem. exit ()Using this model if the method exposure level is weakenedEXPOSE_ALL.

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.