简体   繁体   English

Java StringTemplate遍历复杂对象列表

[英]Java StringTemplate Iterating over List of Complex Object

I have a class like so: 我有一个像这样的课程:

public class Foo {
    private String s;
}

And I have a list of said objects. 我有一个所说的对象清单。 I want to be able to iterate over this list and print out the string property s . 我希望能够遍历此列表并打印出string属性s I have the following code to render the string template: 我有以下代码来呈现字符串模板:

Map<String, Object> params = new HashMap<>();
List<Foo> foos = new ArrayList<>();
foos.add(new Foo("A"));
foos.add(new Foo("B"));
foos.add(new Foo("C"));
params.put("foos", foos);
ST st = new ST("Hello, $data.(\"foos\") : {foo | $foo.s;separator=\", \"$}$", '$', '$');
st.add("data", params);
template = st.render();
System.out.println(template);

But the output is just: 但是输出只是:

Hello, ABC

How do I properly iterate over that list and print it out with comma separation? 如何正确遍历该列表并用逗号分隔打印出来?

Your separator separates the elemnets of foo.s , not those of foo . 您的分隔符分隔foo.s的foo.s ,而不是foofoo.s Try 尝试

ST st = new ST("Hello, $data.(\"foos\") : {foo | $foo.s$};separator=\", \"$", '$', '$');

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

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