Spring MVC 填充@RequestParam Map<String, String>

新手上路,请多包涵

我的 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 变量始终为空

我必须做什么才能填充“测试”变量?

原文由 Christos Loupassakis 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 986
1 个回答

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

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

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

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

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

你会在你的方法

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

原文由 zatopek 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题