解析gsp中的嵌套映射(hashMap)

时间:2012-12-11 23:09:35

标签: 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 = 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被解释为String,因此对.entrySet()的调用会导致异常。

有没有办法通过GSP标签获取子地图的内容?

编辑:

@SérgioMichels: 我正在使用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。

重新编辑:

故障是我的,在进行明确的清理操作时,我将子映射转换为String。循环Xmap.entrySet()工作正常。抱歉,添麻烦了。 Grails摇滚无论版本

1 个答案:

答案 0 :(得分: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>
相关问题