速度找不到方法
Velocity not finding method
我正在开发一个 Web 应用程序,使用 Apache Velocity 作为模板引擎。我希望它显示 HTML5 select,如下所示。
<select class="form-control" id="detailFunction">
#foreach($function in $functions)
<option id="$function.getId()">$function.getTitle()</option>
#end
</select>
我的 Function
class 看起来像这样:
package com.Whosebug;
class Function {
private final int id;
private final String title;
Function(int id, String title) {
this.id = id;
this.title = title;
}
public int getId() {
return this.id;
}
public String getTitle() {
return this.title;
}
}
$functions
是一个 List<Function>
。但是,当我 运行 这段代码时,它说:
Object 'com.Whosebug.Function' does not contain method getId() at /velocity/editor.vm[line 40, column 48]
虽然它显然在那里。即使将 $functions
更改为 Function[]
类型也不会改变任何事情。这可能是什么?
您忘记向函数 class 添加 public
访问修饰符,因此 Velocity 将能够使用它
Velocity will only allow public methods on public classes to be called for security reasons.
声明你的class:
public class Function {
这不是强制性的,但如果您在 velocity 模板中需要它,您可能还应该添加您的构造函数 public 访问权限
public Function(int id, String title) {
我正在开发一个 Web 应用程序,使用 Apache Velocity 作为模板引擎。我希望它显示 HTML5 select,如下所示。
<select class="form-control" id="detailFunction">
#foreach($function in $functions)
<option id="$function.getId()">$function.getTitle()</option>
#end
</select>
我的 Function
class 看起来像这样:
package com.Whosebug;
class Function {
private final int id;
private final String title;
Function(int id, String title) {
this.id = id;
this.title = title;
}
public int getId() {
return this.id;
}
public String getTitle() {
return this.title;
}
}
$functions
是一个 List<Function>
。但是,当我 运行 这段代码时,它说:
Object 'com.Whosebug.Function' does not contain method getId() at /velocity/editor.vm[line 40, column 48]
虽然它显然在那里。即使将 $functions
更改为 Function[]
类型也不会改变任何事情。这可能是什么?
您忘记向函数 class 添加 public
访问修饰符,因此 Velocity 将能够使用它
Velocity will only allow public methods on public classes to be called for security reasons.
声明你的class:
public class Function {
这不是强制性的,但如果您在 velocity 模板中需要它,您可能还应该添加您的构造函数 public 访问权限
public Function(int id, String title) {