Invoke Java method with parameters from Freemarker

删除回忆录丶 提交于 2019-12-23 10:57:47

问题


The following FTL markup works fine for me and calls getWidgets() in my server-side JiveActionSupport object:

<#list widgets! as widget>
  -- do something with widget.sku
</#list>

However, I really need an inner list that depends on a property of widget, something like this:

<#list widgets! as widget>
  <#list manufacturers(widget.sku)! as manufacturer>
  -- do something with manufacturer
  </#list>
</#list>

I have tried to implement the server-side code, as either:

public List<Manufacturer> getManufacturers(int sku);
public List<Manufacturer> getManufacturers(String sku);

But both result in 'Expression manufacturers is undefined at line 123'.

How can you pass parameters to methods of the current JiveActionSupport object? Thanks.


回答1:


The thing that possibly confused you here is that getFoo() can be called as foo, but getFoo(param) can't be called as foo(param), only as getFoo(param). But this is just how JavaBeans work; getFoo() defines a JavaBean property, while getFoo(params) doesn't.

Anyway, if getManufacturers is the method of the data-model (root) object, then (assuming proper object wrapping) you should be able to call it as getManufacturers(param). You don't need to start it with action. in principle.




回答2:


In general, it looks as if you need to do this as follows:

<#list action.getManufacturers("123")! as manufacturer>
  -- do something with manufacturer
</#list>

In particular, while you can use things in FTL to invoke the server-side method getThings(), you need to use action.getThing("123") to invoke the server-side method getThing(String).



来源:https://stackoverflow.com/questions/10486619/invoke-java-method-with-parameters-from-freemarker

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