将列表传递给 freemarker

发布于 2024-12-27 20:10:52 字数 1025 浏览 3 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怎言笑 2025-01-03 20:10:52

看起来您正在为列表中的每个元素创建一个新的 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

不爱素颜 2025-01-03 20:10:52

那是因为您在 for 循环的每次迭代中创建一个新列表。此外,您将在数据模型映射中为同一键添加 4 个列表。应修正如下:

    java.util.List opslist = new ArrayList();

    for(..){
        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

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:

    java.util.List opslist = new ArrayList();

    for(..){
        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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文