Spring MVC - 如何在 Spring 控制器的地图中获取所有请求参数? [英] Spring MVC - How to get all request params in a map in Spring controller?

查看:28
本文介绍了Spring MVC - 如何在 Spring 控制器的地图中获取所有请求参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例网址:

../search/?attr1=value1&attr2=value2&attr4=value4

我不知道 attr1、att2 和 attr4 的名称.

I do not know the names of attr1, att2, and attr4.

我希望能够做类似的事情(或类似的,不在乎,只要我可以访问请求参数名称的 Map -> 值:

I would like to be able to do something like that (or similar, don't care, just as long as I have access to the Map of request param name -> value:

@RequestMapping(value = "/search/{parameters}", method = RequestMethod.GET)
public void search(HttpServletRequest request, 
@PathVariable Map<String,String> allRequestParams, ModelMap model)
throws Exception {//TODO: implement}

如何使用 Spring MVC 实现这一点?

How can I achieve this with Spring MVC?

推荐答案

虽然其他答案是正确的,但直接使用 HttpServletRequest 对象肯定不是Spring 方式".答案实际上是很简单,什么如果您熟悉 Spring MVC,您会期望.

While the other answers are correct it certainly is not the "Spring way" to use the HttpServletRequest object directly. The answer is actually quite simple and what you would expect if you're familiar with Spring MVC.

@RequestMapping(value = {"/search/", "/search"}, method = RequestMethod.GET)
public String search(
@RequestParam Map<String,String> allRequestParams, ModelMap model) {
   return "viewName";
}

这篇关于Spring MVC - 如何在 Spring 控制器的地图中获取所有请求参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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