首页 文章

Spring MVC - 如何在Spring控制器的 Map 中获取所有请求参数?

提问于
浏览
152

示例网址:

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

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

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

@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实现这一目标?

10 回答

  • 31

    虽然其他答案是正确的,但肯定不是"Spring way"直接使用HttpServletRequest对象 . 如果您熟悉Spring MVC,答案实际上是quite simple and what you would expect .

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

    Edit

    已经指出存在(at least as of 3.0)一个纯Spring MVC机制,通过它可以获得这些数据 . 我不会在这里详述,因为它是另一个用户的答案 . 有关详细信息,请参阅@AdamGent's answer,不要忘记对其进行投票 .

    在Spring 3.2文档中,在 RequestMapping JavaDoc页面和 RequestParam JavaDoc页面上都提到了这种机制,但之前只在 RequestMapping 页面中提到过 . 在2.5文档中没有提到这种机制 .

    对于大多数开发人员来说,这可能是首选方法,因为它删除了(至少这个)与servlet-api jar定义的 HttpServletRequest 对象的绑定 .

    /Edit

    您应该可以通过request.getQueryString()访问请求查询字符串 .

    除了getQueryString之外,还可以从request.getParameterMap()中检索查询参数作为Map .

  • 6

    HttpServletRequest对象已经提供了参数映射 . 有关详细信息,请参阅request.getParameterMap() .

  • 2

    你可以简单地使用这个:

    Map<String, String[]> parameters = request.getParameterMap();
    

    这应该工作正常

  • 9

    使用 org.springframework.web.context.request.WebRequest 作为控制器方法中的参数,它提供了方法 getParameterMap() ,其优点是您不会将应用程序紧密到Servlet API,WebRequest是JavaEE模式Context Object的一个示例 .

  • 279

    有两个接口

    • org.springframework.web.context.request.WebRequest

    • org.springframework.web.context.request.NativeWebRequest

    允许通用请求参数访问以及 request/session 属性访问, without ties to the native Servlet/Portlet API .

    例:

    @RequestMapping(value = "/", method = GET)
    public List<T> getAll(WebRequest webRequest){
        Map<String, String[]> params = webRequest.getParameterMap();
        //...
    }
    

    附: There are有关可用作Controller参数的参数的文档 .

  • 4

    这是 getting request params in a Map. 的简单示例

    @RequestMapping(value="submitForm.html", method=RequestMethod.POST)
         public ModelAndView submitForm(@RequestParam Map<String, String> reqParam) 
           {
              String name  = reqParam.get("studentName");
              String email = reqParam.get("studentEmail");
    
              ModelAndView model = new ModelAndView("AdmissionSuccess");
              model.addObject("msg", "Details submitted by you::
              Name: " + name + ", Email: " + email );
           }
    

    在这种情况下,它将分别将studentName和studentEmail的值与名称和电子邮件变量绑定 .

  • 1

    我可能会迟到,但根据我的理解,你正在寻找这样的事情:

    for(String params : Collections.list(httpServletRequest.getParameterNames())) {
        // Whatever you want to do with your map
        // Key : params
        // Value : httpServletRequest.getParameter(params)                
    }
    
  • 13
    @SuppressWarnings("unchecked")
            Map<String,String[]> requestMapper=request.getParameterMap();
            JsonObject jsonObject=new JsonObject();
            for(String key:requestMapper.keySet()){
                jsonObject.addProperty(key, requestMapper.get(key)[0]);
            }
    

    //所有参数都将存储到jsonObejct中

  • 5

    查询参数和路径参数之间存在根本区别 . 它是这样的: www.your_domain?queryparam1=1&queryparam2=2 - 查询参数 . www.your_domain/path_param1/entity/path_param2 - 路径参数 .

    令我惊讶的是,在Spring MVC世界中,很多人将一个人混淆了另一个 . 虽然查询参数更像是搜索的标准,但路径参数很可能唯一地标识资源 . 话虽如此,但这并不意味着您的URI中不能有多个路径参数,因为资源结构可以嵌套 . 例如,假设您需要特定人员的特定汽车资源:

    www.my_site/customer/15/car/2 - 寻找第15位客户的第二辆车 .

    将所有路径参数放入 Map 的用例是什么?当您查看URI本身时,路径参数没有“键”, Map 中的那些键将从您的@Mapping注释中获取,例如:

    @GetMapping("/booking/{param1}/{param2}")
    

    从HTTP / REST角度来看,路径参数确实无法投影到 Map 上 . 在我看来,这完全取决于Spring的灵活性以及他们想要容纳任何开发人员的愿望 .

    我永远不会使用路径参数的 Map ,但它对查询参数非常有用 .

相关问题