How do I call java methods on an object from a FreeMarker template?

走远了吗. 提交于 2019-11-30 10:43:33

FreeMarker allows invoking methods that were made available through the model from within expressions.

Assuming your object has been exposed as myBean you can invoke the method as follows:

<#list myBean.getunits("myType") as unit>
  do stuff with ${unit}
</#list>

You don't have to use <list>, of course, it's just there as an example since your method returns a list.

As ChssPly76 said, you can just peform the method call from within a Freemarker template, as long as you expose the object in the model.

But it's important to keep in mind that if your method returns NULL (for whatever reason), you are going to get a confusing

Expression myBean.getunits() is undefined on line ....

To avoid this, you should better use myBean.getunits(...)! (notice the exclamation point).

Learn more about how Freemarker handles nulls here: http://freemarker.org/docs/dgui_template_exp.html#dgui_template_exp_missing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!