In a Freemarker FTL file, I want to call StringUtils.capatilize(myString). For example:
<p>You selected ${selectionString}.</p>
<p>${StringUtils.capatilize(selectionString)} is great.</p>
Can I somehow import org.apache.commons.lang3.StringUtils?
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)}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With