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

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

问题描述

示例网址:

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

我不知道attr1,att2和attr4的名称。



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



'pre> @RequestMapping(值= /搜索/ {参数},方法= RequestMethod.GET)
公共无效搜索(HttpServletRequest的请求,
@PathVariable地图<字符串,字符串> allRequestParams,ModelMap模型)
抛出异常{// TODO:实施}

如何使用Spring MVC实现这一目标?

解决方案

虽然其他答案是正确的,但肯定是不是春方式直接使用HttpServletRequest对象。其实答案非常简单,你会期望什么?

  @RequestMapping(值= { /搜索/, /搜索},方法= RequestMethod.GET)
公共字符串搜索(
@RequestParam地图<字符串,字符串> allRequestParams,ModelMap模型){
返回viewName;
}


Sample URL:

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

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

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}

How can I achieve this with 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天全站免登陆