将列表传递给 freemarker
Java 文件
for(..){
java.util.List opslist = new ArrayList();
opr.setOperationName(operation.getName()); //gets operation name (iterate and get n no of names) sets it to opr
System.out.println(opr.getOperationName());//gets all the set values n prints it(jus to chack that all values r getting set)
opslist.add(opr.getOperationName());//putting those valies into a list
datamodel.put("opslist", opslist.toArray(new String[]{}));//putting it into a hash map with key as opslist and value as opslist object
}
Freemarker 模板
<#list opslist as x> //read the values from the key "opslist" (gets only one value)
${x} //print values one by one(it prints only one value) </#list>
java 文件的输出是
get
set
value
usage
Freemarket 模板的输出是
usage
为什么只得到最后一个值打印出来了吗?
有人可以告诉我在 freemarker 模板中执行此操作的正确方法吗?
Java File
for(..){
java.util.List opslist = new ArrayList();
opr.setOperationName(operation.getName()); //gets operation name (iterate and get n no of names) sets it to opr
System.out.println(opr.getOperationName());//gets all the set values n prints it(jus to chack that all values r getting set)
opslist.add(opr.getOperationName());//putting those valies into a list
datamodel.put("opslist", opslist.toArray(new String[]{}));//putting it into a hash map with key as opslist and value as opslist object
}
Freemarker Template
<#list opslist as x> //read the values from the key "opslist" (gets only one value)
${x} //print values one by one(it prints only one value) </#list>
output of the java file is a
get
set
value
usage
output of Freemarket template is
usage
Why is only last value getting printed?
Can someone tell me the right way to do inside freemarker template?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来您正在为列表中的每个元素创建一个新的 opslist。这样只有最后一个元素被传递给 freemarker。
只需将 List opslist = new ArrayList();在 for 循环前面
it looks like you are creating a new opslist for each element in your list. in this way only the last element is passed to freemarker.
just put List opslist = new ArrayList(); in front of the for loop
那是因为您在 for 循环的每次迭代中创建一个新列表。此外,您将在数据模型映射中为同一键添加 4 个列表。应修正如下:
That's because you are creating a new list in every iteration of the for loop. Also, you are adding 4 lists for the same key, in datamodel map. It should be corrected as below: