java Freemarker:在哈希中迭代嵌套列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27264613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:31:00  来源:igfitidea点击:

Freemarker: iterating nested list in hash

javajakarta-eefreemarker

提问by pat.inside

I want to iterate a List nested in a Map, the data structure is like:

我想迭代一个嵌套在 Map 中的 List,数据结构如下:

Map<Integer, List<Integer>> groups = new TreeMap<>()
// Some code else to put values into groups ...

Freemarker template:

自由标记模板:

<#list groups?keys as groupKey>
    ${groupKey}    // It's OK here.
    <#list groups[groupKey] as item>  // Exception threw here, detail message is pasted below
        ${item}
    </#list>
</#list>

Detail exception message:

详细异常信息:

FreeMarker template error: For "...[...]" left-hand operand: Expected a sequence or string or something automatically convertible to string (number, date or boolean), but this evaluated to an extended_hash (wrapper: f.t.SimpleHash): ==> groups

FreeMarker 模板错误:对于“...[...]”左手操作数:需要一个序列或字符串或自动转换为字符串(数字、日期或布尔值)的东西,但这被评估为extended_hash(包装器:ftSimpleHash) : ==> 组

So, what is the problem?

那么,问题是什么?

P.S.

聚苯乙烯

I have tried groups.get(groupKey)instead of groups[groupKey], it throws a new Exception stack:

我尝试过,groups.get(groupKey)而不是groups[groupKey],它抛出一个新的异常堆栈:

java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
java.lang.String.compareTo(String.java:108)
java.util.TreeMap.getEntry(TreeMap.java:346)
java.util.TreeMap.get(TreeMap.java:273)
freemarker.template.SimpleHash.get(SimpleHash.java:160)
freemarker.core.Dot._eval(Dot.java:40)
freemarker.core.Expression.eval(Expression.java:76)

采纳答案by ddekany

The problem in the original question is that FTL's hash type is not like Map. It's a collection of "variables", that is, the keys must be String-s. (Even that ?keysworks is a glitch in BeansWrapper... though now it comes handy.) Since the key is a number, FTL assumes that you want to get an item from a sequence (a Listor array), or that you want to get a character from a string, hence the original error message.

原题的问题是FTL的hash类型不像Map. 它是“变量”的集合,即键必须是String-s。(即使这样?keys工作也是一个小问题BeansWrapper......尽管现在它很方便。)由于键是一个数字,FTL 假设您想从序列(aList或数组)中获取一个项目,或者您想获取一个字符串中的字符,因此是原始错误消息。

The solution is using the Java API-s, like getin Dev-an's answer. (On the long term FTL meant to introduce the map type, so all this problems with non-string keys will end, but who knows when that will be...)

解决方案是使用 Java API-s,就像get在 Dev-an 的回答中一样。(从长远来看,FTL 意味着引入地图类型,所以所有这些非字符串键的问题都会结束,但谁知道那会是什么时候......)

Update:Since 2.3.22 there's ?apito access the Java API of objects, like myMap?api.get(nonStringKey). However, it's by default not allowed (see the api_builtin_enabledconfiguration setting and more in the Manual: http://freemarker.org/docs/ref_builtins_expert.html#ref_buitin_api_and_has_api). Also note that as Java maps are particular about the numerical type, if the key is not an Integercoming from a Java, you have to use myMap?api.get(myNumericalKey?int).

更新:从 2.3.22 开始,?api可以访问对象的 Java API,例如myMap?api.get(nonStringKey). 但是,默认情况下是不允许的(请参阅api_builtin_enabled手册中的配置设置和更多内容:http: //freemarker.org/docs/ref_builtins_expert.html#ref_buitin_api_and_has_api)。另请注意,由于 Java 映射特别关注数字类型,如果键不是Integer来自 Java,则必须使用myMap?api.get(myNumericalKey?int).

回答by Dev-an

Try the following:

请尝试以下操作:

<#list groups?keys as groupKey>
    ${groupKey}
    <#list groups.get(groupKey) as item>
        ${item}
    </#list>
</#list>