gpt4 book ai didi

grails - 在gsp中解析嵌套 map (hashMap)

转载 作者:行者123 更新时间:2023-12-02 14:45:08 25 4
gpt4 key购买 nike

全部在标题中。
我一直在尝试解析在示例 Controller 中构建的嵌套Map,如下所示:

    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])
}

在GSP中,我尝试获取如下的childMaps元素:
    ... html / gsp code ...
<table>
<g:each in="${motherMap.entrySet()}" var="entry">
<g:if test="${entry.key != 'mm_key2'}">
<g:each in="${entry.value.entrySet()}" var="childMap">
<tr>
<td>${childMap.key}</td>
<td>${childMap.value}</td>
</tr>
</g:each>
</g:if>
</g:each>
</table>
... html / gsp code ...

但是我在处理页面时遇到了麻烦。 entry.value被解释为字符串,因此对 .entrySet()的调用会导致异常。

是否可以通过GSP标签获取子 map 的内容?

编辑:

@塞吉奥·米歇尔斯(SérgioMichels):
我在Groovy 1.7中使用了Grails 1.3.7(已强加)。

这是堆栈跟踪:
    [ServiceBox] ERROR 2012-12-11 22-12-40 - Error processing GroovyPageView: No signature of method: java.lang.String.entrySet() is applicable for argument types: () values: []
Possible solutions: getBytes(), every()
groovy.lang.MissingMethodException: No signature of method: java.lang.String.entrySet() is applicable for argument types: () values: []
Possible solutions: getBytes(), every()
at F__SES_thePage_gsp$_run_closure2_closure3.doCall(thePage.gsp:54)
at F__SES_thePage_gsp$_run_closure2.doCall(thePage.gsp:51)
at F__SES_thePage_gsp$_run_closure2.doCall(thePage.gsp)
at F__SES_thePage_gsp.run(thePage.gsp:65)
at com.ircem.filter.CharsetFilter.doFilter(CharsetFilter.java:24)
at java.lang.Thread.run(Thread.java:595)
[ServiceBox] ERROR 2012-12-11 22-12-40 - Exception occurred when processing request: [GET] /ServiceBox/getthePage
Stacktrace follows:
org.codehaus.groovy.grails.web.pages.exceptions.GroovyPagesException: Error processing GroovyPageView: No signature of method: java.lang.String.entrySet() is applicable for argument types: () values: []
Possible solutions: getBytes(), every()
at com.ircem.filter.CharsetFilter.doFilter(CharsetFilter.java:24)
at java.lang.Thread.run(Thread.java:595)
Caused by: groovy.lang.MissingMethodException: No signature of method: java.lang.String.entrySet() is applicable for argument types: () values: []
Possible solutions: getBytes(), every()
at F__SES_thePage_gsp$_run_closure2_closure3.doCall(thePage.gsp:54)
at F__SES_thePage_gsp$_run_closure2.doCall(thePage.gsp:51)
at F__SES_thePage_gsp$_run_closure2.doCall(thePage.gsp)
at F__SES_thePage_gsp.run(thePage.gsp:65)
... 2 more

@tim_yates:
删除 .entrySet()调用会导致 groovy.lang.MissingPropertyException: No such property: key for class: java.lang.String。 Controller 的HashMap在 View 中解释为String。

重新编辑:

错误是我的,在执行显式sanitze操作时,我将子映射转换为String。循环 Xmap.entrySet()可以正常工作。抱歉,添麻烦了。无论版本如何,Grails都会晃动

最佳答案

试试这个:

<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>

关于grails - 在gsp中解析嵌套 map (hashMap),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13830062/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com