Java 将列表传递给freemarker

Java 将列表传递给freemarker,java,list,freemarker,Java,List,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

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
}           
<#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>
自由标记模板

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
}           
<#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>
自由市场模板的输出为

usage
为什么只打印最后一个值?

有人能告诉我在freemarker模板中的正确方法吗?

看起来您正在为列表中的每个元素创建一个新的opslist。这样,只将最后一个元素传递给freemarker。
只需将列表opslist=newarraylist();在for循环前面

这是因为在for循环的每次迭代中都要创建一个新列表。另外,在DataModelMap中,您正在为同一个键添加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
在表单中添加代码。