Explanation of @RequestParam for SpringMVC

  • 2020-10-07 18:38:50
  • OfStack

After SpringMVC4.2, there are four parameters inside RequestParam:

1, String name

2, String value

3, boolean required

4, String defaultValue

name and value are nicknames for each other, so there is no difference between the two. Personally, I prefer name because it has some features that make the name name more intuitive, as explained below.

Let's first look at the definition of the first mapping method:


@RequestMapping("/paramTest0")
 public @ResponseBody String paramTest(Long id){
  String result = "";
  result += id;
  return result;
 }

(1) then I enter in your browser's address bar: http: / / localhost: 8080 / test hello/paramTest0

Browser display: null

One of the features of SpringMVC is that SpringMVC assigns id to null when no parameters and values are entered in the browser. Note that null is not 0, so it is best not to use the base type for the parameters.

(2) in the browser input: http: / / localhost: 8080 / test/hello/paramTest0 & # 63; userName=zhang & userName=li & id=9 & userName=shit

Browser display: 9

Indicates that the browser only needs parameters for input, regardless of whether there are extra parameters, and there is no specified order.

(3) in the browser input: http: / / localhost: 8080 / test/hello/paramTest0 & # 63; id=6

It is shown as: 6

I'm not going to explain that.

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # delimiter # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

Consider the definition of the second mapping method:


@RequestMapping("/paramTest")
 public @ResponseBody String paramTest(@RequestParam(name="userName", required = false) String[] userNames, Long id){
  String result = "";
  if(userNames != null){
   for(int i=0;i<userNames.length;i++){
    result += userNames[i] + "#";
   }
  }
  result += id;
  return result;
 }

(1) and then input to the browser: http: / / localhost: 8080 / test/hello/paramTest & # 63; userName=zhang & userName=li & id=5 & userName=fuck

Displayed as: zhang#li#fuck#5

First, the parameter part of URL request can have multiple pairs of parameter names like userName above, and they can be separated by other parameters (id=5 is used above) without affecting the parameter values of these parameter names to form an "array".

The value of the same parameter name will be changed by the browser to something like "userName="zhang,li,fuck", telling the server that this is an array and that the order of the element values corresponds to the order in the URL request.

Then look at the method argument that says userNames instead of userName, but it still shows up correctly because the value of RequestParam's name or value attribute is userName and

The name of the parameter passed in from the browser corresponds (which I think is why name is a bit more intuitive than value), and the annotated parameter userNames is

This annotates the variable to be "assigned" by "userName" (or RequestParam as a kind of mediation to direct the parameter values from the client request to the parameters of the corresponding request mapping method, userNames).

Also note that 1 @RequestParam can only annotate 1 parameter, that is, the following Long id does not have this annotation.

required in RequestParam refers to whether the parameter must be provided by the client, while defaultValue refers to what the default value of the parameter is if it is not provided (so required=true, defaultValue="xxx" is meaningless).


Related articles: