Announcement Announcement Module
Collapse
No announcement yet.
How to get all request paramaters in a map in a Spring MVC Contoller? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to get all request paramaters in a map in a Spring MVC Contoller?

    Hi,

    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<String,String> of request param name -> value:

    @RequestMapping(value = "/search/{parameters}", method = RequestMethod.GET)
    public void search(HttpServletRequest request, @PathVariable Map<Spring,Spring> allRequestParams, ModelMap model) throws Exception {
    // my Map allRequestParams contains: attr1->value1, attr2->value2, attr4->value4

    }

    How can i achieve this with Spring MVC?


    Thanks

    MDB
    Last edited by podroznik; Sep 5th, 2011, 04:24 PM.

  • #2
    Can you do something like this?

    Code:
    	private void displayRequestParam(HttpServletRequest request){
    		Enumeration<String>enumName = request.getParameterNames();
    		logger.debug("Method "+request.getMethod());
    		while(enumName.hasMoreElements()){
    			String name  = enumName.nextElement();
    			logger.debug("ParamerName: "+name+"-->"+request.getParameter(name));
    		}
    		logger.debug("End of log");
    	}

    Comment


    • #3
      Thanks. Sure I can, in fact I did before I posted. I was wondering if Spring would be of any help, as this seems like a common use case.

      Comment


      • #4
        Cześć Podróżnik

        You can always try:
        Code:
        org.springframework.web.util.WebUtils.getParametersStartingWith(ServletRequest, String)
        Spring has much more these handy utilities

        regards
        Grzegorz Grzybek

        Comment


        • #5
          Well the HttpServletRequest has a method to get all parameters like a map, it is (surprisingly) called getParameterMap. Or instead use spring WebRequest which also has a getParameterMap.

          Comment


          • #6
            As always - the simpliest methods are the best

            Comment

            Working...
            X