how to capture multiple parameters using @RequestParam using spring mvc?

前端 未结 6 1417
独厮守ぢ
独厮守ぢ 2020-11-30 01:34

Suppose a hyperlink is clicked and an url is fired with the following parameter list myparam=myValue1&myparam=myValue2&myparam=myValue3 . Now how can I

相关标签:
6条回答
  • 2020-11-30 02:14

    As of Spring 3.0, you can also use MultiValueMap to achieve this:

    A rudimentary example would be:

    public String someMethod(@RequestParam MultiValueMap<String,String> params) {
    
        final Iterator<Entry<String, List<String>>> it = params.entrySet().iterator();
    
        while(it.hasNext()) {
            final String k = it.next().getKey();
            final List<String> values = it.next().getValue();
        }
    
        return "dummy_response";
    
    }
    
    0 讨论(0)
  • 2020-11-30 02:16

    Spring mvc can support List<Object>, Set<Object> and Map<Object> param, but without @RequestParam.

    Take List<Object> as example, if your object is User.java, and it like this:

    public class User {
        private String name;
        private int age;
    
        // getter and setter
    }
    

    And you want pass a param of List<User>, you can use url like this

    http://127.0.0.1:8080/list?users[0].name=Alice&users[0].age=26&users[1].name=Bob&users[1].age=16
    

    Remember to encode the url, the url after encoded is like this:

    http://127.0.0.1:8080/list?users%5B0%5D.name=Alice&users%5B0%5D.age=26&users%5B1%5D.name=Bob&users%5B1%5D.age=16
    

    Example of List<Object>, Set<Object> and Map<Object> is displayed in my github.

    0 讨论(0)
  • 2020-11-30 02:25

    It seems you can't get

    Map<String,String>
    

    because all your params have same name "myparam"

    Try this instead:

    public ModelAndView method(@RequestParam("myparam") List<String> params) { }
    
    0 讨论(0)
  • 2020-11-30 02:28

    To get all parameters at once try this:

    public ModelAndView postResultPage(@RequestParam MultiValueMap<String, String> params)
    

    This feature is described in the @RequestParam java doc (3. Paragraph):

    Annotation which indicates that a method parameter should be bound to a web request parameter. Supported for annotated handler methods in Servlet and Portlet environments.

    If the method parameter type is Map and a request parameter name is specified, then the request parameter value is converted to a Map assuming an appropriate conversion strategy is available.

    If the method parameter is Map<String, String> or MultiValueMap<String, String> and a parameter name is not specified, then the map parameter is populated with all request parameter names and values.

    0 讨论(0)
  • 2020-11-30 02:30

    If anyone is trying to do the same in Spring Boot, use RequestBody in place of RequestParam

    0 讨论(0)
  • 2020-11-30 02:34
    @RequestMapping(value = "users/newuser", method = RequestMethod.POST)   
    public String saveUser(@RequestParam Map<String,String> requestParams) throws Exception{
       String userName=requestParams.get("email");
       String password=requestParams.get("password");
    
       //perform DB operations
    
       return "profile";
    }
    

    You could use RequestParam in the above mentioned manner.

    0 讨论(0)
提交回复
热议问题