【问题标题】:Access object property in a list in velocity template访问速度模板列表中的对象属性
【发布时间】:2017-10-27 12:56:20
【问题描述】:

我想以速度访问对象属性

我有以下。

public class myObject(){
    @key("name")
    private String name;

    @key("other")
    private String other;

    /*
      getters and setters here
    */
}

然后我有另一个包含 myObject 对象列表的类

public class testClass(){
    @key("objectList")
    private List<myObject> randomlist;
}

如何访问 myObject 列表的 nameother ? 我的速度如下所示,但不起作用

#macro( getListContent $tag $tag2 $listName)
#foreach($object in [0..$listName-size])
<$tag1>$object-name</$tag1>
<$tag2>$object-other</$tag2>
#end
#end

我终于有了

#getListContent("name" "other" $testClass.get("objectList"))

但这不起作用。我如何访问使用注释 @key 映射的对象属性一些帮助将非常有用。

【问题讨论】:

    标签: java xml list templates velocity


    【解决方案1】:

    您应该看到macro 文档,在您的 foreach 语句中您需要放置列表对象,

    name 和 other 也是私有的,因此无法访问,

    但你可以使用public getters 方法:

    #macro( getListContent $tag $tag2 $listName)
    #foreach($object in $listName)
    <$tag1>$object.getName()</$tag1>
    <$tag2>$object.getOther()</$tag2>
    #end
    #end
    

    【讨论】:

      猜你喜欢
      • 2018-01-17
      • 2016-06-23
      • 2012-05-01
      • 1970-01-01
      • 2011-04-19
      • 2015-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多