简体   繁体   English

在力度模板的列表中访问对象属性

[英]Access object property in a list in velocity template

I want to access an object property in velocity 我想以速度访问对象属性

I have the following. 我有以下几点。

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

    @key("other")
    private String other;

    /*
      getters and setters here
    */
}

Then i have another class which contains a list of myObject objects 然后我有另一个类,其中包含myObject对象的列表

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

How can i access name and other of myObject list ? 如何访问myObject列表的nameother name My velocity looks as follow but doesn't work 我的速度如下,但不起作用

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

and finally i have 最后我有

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

but this doesn't work. 但这不起作用。 How can i access object properties that are mapped with an annotation @key Some help would be really useful. 我如何访问用注解@key映射的对象属性有些帮助将非常有用。

You should see macro documentation, In your foreach statement you need to put the list object, 您应该会看到文档,在foreach语句中,您需要放入列表对象,

Also name and other are private so they are unreachable, 名字和其他名字也是私人的,因此无法访问,

But you can use public getters methods: 但是您可以使用public方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM