使用来自 Freemarker 的参数调用 Java 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10486619/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 01:15:54  来源:igfitidea点击:

Invoke Java method with parameters from Freemarker

javafreemarker

提问by jarmod

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

以下 FTL 标记对我来说很好用,并在我的服务器端 JiveActionSupport 对象中调用 getWidgets():

<#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'.

但两者都导致“表达式制造商在第 123 行未定义”。

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

如何将参数传递给当前 JiveActionSupport 对象的方法?谢谢。

回答by ddekany

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.

这里可能让您感到困惑的是,getFoo()可以称为 as foo,但getFoo(param)不能称为 as foo(param),只能称为getFoo(param)。但这正是 JavaBeans 的工作方式;getFoo()定义了一个 JavaBean 属性,而getFoo(params)没有。

Anyway, if getManufacturersis 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.

无论如何,如果getManufacturers是数据模型(根)对象的方法,那么(假设正确的对象包装)您应该能够将其称为getManufacturers(param). 原则上您不需要从它开始action.

回答by jarmod

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 thingsin FTL to invoke the server-side method getThings(), you need to use action.getThing("123")to invoke the server-side method getThing(String).

特别是,虽然您可以使用FTL 中的事物来调用服务器端方法getThings(),但您需要使用action.getThing("123")来调用服务器端方法getThing(String)