1

这一切都在标题中。我一直在尝试解析在我的示例控制器中构建的嵌套地图,如下所示:

    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 标签获取子地图的内容?

编辑:

@Sérgio Michels:我将 Grails 1.3.7 与 Groovy 1.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。控制器的 HashMap 在视图中被解释为 String。

重新编辑:

错误是我的,在进行明确的清理操作时,我将子地图转换为字符串。循环Xmap.entrySet()工作正常。抱歉,添麻烦了。无论版本如何,Grails 都令人震撼

4

1 回答 1

1

尝试这个:

<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>
于 2012-12-12T10:56:09.770 回答