如何从FreeMarker模板调用对象上的java方法? [英] How do I call java methods on an object from a FreeMarker template?

查看:1063
本文介绍了如何从FreeMarker模板调用对象上的java方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以调用从Freemarker模板中获取参数的方法?

Is it possible to call a method that takes parameters from a Freemarker template?

我有一个对象模型,我试图用Freemarker渲染到网页页。其中一个对象有一个获取其内容子列表的方法 - 获取用于过滤列表的参数:

I have an object model that I'm trying to render with Freemarker into a web page. One of the objects has a method to get a sublist of it's contents - taking a parameter that is used to filter the list:

public List getunits (final String type);

我在JSP中知道你不能直接这样做,但是你可以编写自定义函数来实现达到你想要的效果。你如何在Freemarker中解决这个问题?编写自定义函数是一样的吗?或者有没有办法实际调用这种函数?

I know in JSP you can't do this directly, but you can write custom functions that will allow you to achieve the result you want. How do you solve this in Freemarker? Is it the same with writing custom functions? Or is there some way of actually calling this kind of function?

推荐答案

FreeMarker允许调用通过模型提供的方法在表达式中。

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

假设您的对象已公开为 myBean 您可以按如下方式调用该方法:

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>

您不必使用< list> ,当然,它只是作为一个例子,因为你的方法返回一个列表。

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

这篇关于如何从FreeMarker模板调用对象上的java方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆