Freemarker: call a static util method from a template file (*.ftl)
You can't #import
a class, only other templates.
Note that you can achieve a similar result with #assign StringUtils=statics['org.apache.commons.lang3.StringUtils']
, as far as you add objectWrapper.getStaticModels()
as statics
to the set of shared variables in the Configuration
or add it to the data-model.
First, add these code to your Controller
:
BeansWrapper wrapper = new BeansWrapper(new Version(2,3,27));
TemplateModel statics = wrapper.getStaticModels();
model.addAttribute("statics", statics);
And then, in your .ftl
file, define the class like this:
<#assign YourUtilClass=statics['com.springboot.util.YourUtilClass']>
(The path including in ['']
is the class' path)
Finally, you can access your static method like this:
${YourUtilClass.yourMethod(someParams)}