Grails 在gsp中解析嵌套映射(hashMap)
都在标题里。 我一直在尝试解析在示例控制器中构建的嵌套映射,如下所示:Grails 在gsp中解析嵌套映射(hashMap),grails,groovy,gsp,Grails,Groovy,Gsp,都在标题里。 我一直在尝试解析在示例控制器中构建的嵌套映射,如下所示: def index() { Map<String, Object> motherMap = new HashMap<String, Object>() Map<String, String> childMap1 = new HashMap<String, String>() Map<String, String> childMap2
def index() {
Map<String, Object> motherMap = new HashMap<String, Object>()
Map<String, String> childMap1 = new HashMap<String, String>()
Map<String, String> childMap2 = new HashMap<String, String>()
Map<String, String> childMap3 = new HashMap<String, String>()
childMap1.put("cm1_key1", "cm1_value1")
childMap1.put("cm1_key2", "cm1_value2")
childMap1.put("cm1_key3", "cm1_value3")
childMap2.put("cm2_key1", "cm2_value1")
childMap2.put("cm2_key2", "cm2_value2")
childMap2.put("cm2_key3", "cm2_value3")
childMap3.put("cm3_key1", "cm3_value1")
childMap3.put("cm3_key2", "cm3_value2")
childMap3.put("cm3_key3", "cm3_value3")
motherMap.put("mm_key1", childMap1)
motherMap.put("mm_key2", childMap2)
motherMap.put("mm_key3", childMap3)
render (view: "thePage", model:[motherMap: motherMap])
}
@蒂姆·耶茨:
删除.entrySet()
调用会导致groovy.lang.MissingPropertyException:没有这样的属性:类的键:java.lang.String
。控制器的HashMap在视图中被解释为字符串
重新编辑:
错误是我的,在执行显式清理操作时,我将子映射转换为字符串。在Xmap.entrySet()
上循环工作正常。抱歉给你添麻烦了。Grails rocks无论版本如何尝试以下方法:
<g:each in="${motherMap}" var="motherMapEntry">
<p>
<g:if test="${motherMapEntry.key != 'mm_key2'}">
<g:each in="${motherMapEntry.value}" var="entry">
<p>${entry.key} -> ${entry.value}
</g:each>
</g:if>
</g:each>
${entry.key}->${entry.value}
试试这个:
<g:each in="${motherMap}" var="motherMapEntry">
<p>
<g:if test="${motherMapEntry.key != 'mm_key2'}">
<g:each in="${motherMapEntry.value}" var="entry">
<p>${entry.key} -> ${entry.value}
</g:each>
</g:if>
</g:each>
${entry.key}->${entry.value}
您使用的是哪种版本的Grails?张贴stacktrace,因为对我来说效果很好。如果删除.entrySet()
调用(我相信您不需要它们中的任何一个),会发生什么?如果Roman的答案不起作用,您可以为此创建标记库。我在Grails2.1.0中测试并成功了。您使用的是Grails的巫婆版本吗?张贴stacktrace,因为对我来说效果很好。如果删除.entrySet()
调用(我相信您不需要它们中的任何一个),会发生什么?如果Roman的答案不起作用,您可以为此创建标记库。我在Grails2.1.0中进行了测试并工作了。
<g:each in="${motherMap}" var="motherMapEntry">
<p>
<g:if test="${motherMapEntry.key != 'mm_key2'}">
<g:each in="${motherMapEntry.value}" var="entry">
<p>${entry.key} -> ${entry.value}
</g:each>
</g:if>
</g:each>