Spring MVC 填充@RequestParam Map<String, String> [英] Spring MVC populate @RequestParam Map&lt;String, String&gt;

查看:81
本文介绍了Spring MVC 填充@RequestParam Map<String, String>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Spring MVC @Controller 中有以下方法:

I have the following method in my Spring MVC @Controller :

@RequestMapping(method = RequestMethod.GET)
public String testUrl(@RequestParam(value="test") Map<String, String> test) {   
    (...)
}

我这样称呼它:

http://myUrl?test[A]=ABC&test[B]=DEF

然而,测试"RequestParam 变量始终为空

However the "test" RequestParam variable is always null

为了填充测试"变量我必须做什么?

What do I have to do in order to populate "test" variable ?

推荐答案

详见此处https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestParam.html

如果方法参数为 Map 或 MultiValueMap 且未指定参数名称,则使用所有请求参数名称和值填充 map 参数.

If the method parameter is Map or MultiValueMap and a parameter name is not specified, then the map parameter is populated with all request parameter names and values.

所以你会像这样改变你的定义.

So you would change your definition like this.

@RequestMapping(method = RequestMethod.GET)
public String testUrl(@RequestParam Map<String, String> parameters) 
{   
  (...)
}

如果您在参数中调用了 url http://myUrl?A=ABC&B=DEF

And in your parameters if you called the url http://myUrl?A=ABC&B=DEF

你会在你的方法中

parameters.get("A");
parameters.get("B");

这篇关于Spring MVC 填充@RequestParam Map<String, String>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆