简体   繁体   English

如何将HashMap传递给gsp页面然后再传回控制器?

[英]How can I pass a HashMap to gsp page and then back to the controller?

I have values that I need to pass around in the structure of 我有值,我需要在结构中传递

type1 : value1, value2, value3
type2 : value4, value5, value6
type3 : value7, value8, value8

So I made a HashMap<String, ArrayList<String>>() in my controller and I pass it to my gsp file like this: 所以我在我的控制器中创建了一个HashMap<String, ArrayList<String>>() ,然后将它传递给我的gsp文件,如下所示:

def queryMap = new HashMap<String, ArrayList<String>>()
//fill in data
render(template: "/item/myPage", model: [queryMap: queryMap])

Then in my gsp page I would like to pass it back to my controller like this: 然后在我的gsp页面中,我想将它传回给我的控制器,如下所示:

<g:link action="myNewPage" controller="item" params='[queryMap: "${queryMap}"]'>fun</g:link>

Then in myNewPage controller method I would like to get the map like this: 然后在myNewPage控制器方法中我想得到这样的地图:

def queryMap = params.queryMap as HashMap

But for some reason my map is not getting passed around correctly? 但由于某种原因,我的地图没有正确传递? How can I properly pass a map with a list to my gsp file then back to my controller? 如何正确地将带有列表的地图传递给我的gsp文件然后再传回我的控制器?

If you need nay more information let me know. 如果您需要更多信息,请告诉我。

Use as below without interpolating queryMap (without ${} ) 使用如下,不插入queryMap (不含${}

<g:link action="myNewPage" 
        controller="item" 
        params="[queryMap: queryMap]">fun</g:link>

Moreover, you do not need to infer the type as HashMap , by default it is. 此外,您不需要将类型推断as HashMap ,默认情况下也是如此。 I would also agree to @Isammoc about coming up with an appropriate data model (by using form etc.) instead of flowing over the model back and forth. 我也同意@Isammoc关于提出适当的数据模型(通过使用表格等)而不是来回流过模型。

You may try to serialize the whole map and then pass it around and deserialize when you need it. 您可以尝试序列化整个地图,然后传递它并在需要时反序列化。 You could convert the whole Map into JSON and pass it as a String. 您可以将整个Map转换为JSON并将其作为String传递。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM