Sunday, 10 July 2011

Accessing Request Parameters using Spring 3 MVC

One of the neat things you can do in Spring 3 MVC is to map request parameters into specific controller method input arguments. This is done by applying the @RequestParam annotation to your controller method signature:

  @RequestMapping(value = "/help/detail", method = RequestMethod.GET)
 
public String displaySomeHelpDetail(@RequestParam("uuid") String uuid, Model model) {
   
return "view.name";
 
}

In the above example, the @RequestMapping annotation routes all GET requests with the URL /help/detail to your method. Using @RequestParam(“uuid”) means that Spring will then grab the uuid value from the request string and attach it to the String uuid method argument. Knowing this, you can see that the method above will process all GET requests with the following format: /help/detail?uuid=SomeValue, where SomeValue becomes the uuid argument.

In the above, the uuid is part of the @RequestParam’s value attribute and hence, the method signature could have been written as:

  @RequestMapping(value = "/help/detail", method = RequestMethod.GET)
 
public String displayMyFirstHelpDetail(@RequestParam(value="uuid") String uuid, Model model) {
   
return "view.name";
 
}

Indeed, @RequestParam has two other optional attributes: required and defaultValue. required is a boolean that’s applied to the value attribute that defaults to true. When true, then a exception is thrown if the request parameter is missing from the URL request string. When required is false, then an exception is not thrown and the input argument is set to null. An example of how to use required is:

  @RequestMapping(value = "/help/detail", method = RequestMethod.GET)
 
public String displaySomeOtherHelpDetail(@RequestParam(value="uuid", required=false) String uuid, Model model) {
   
return "view.name";
 
}

In the above code, a request URL string of /help/detail?uuid=SomeValue will ensure that uuid is set to SomeValue. When the request param is missing and the URL request string is: /help/detail, then the uuid method argument will be set to null.

Using defaultValue automatically sets required to false, and inserts a ‘default value’ into your input argument when the request parameter is missing from your URL. An example of how to use defaultValue is:

  @RequestMapping(value = "/help/detail", method = RequestMethod.GET)
 
public String displayYetOtherHelpDetail(@RequestParam(value="uuid", defaultValue="hello") String uuid, Model model) {
   
return "view.name";
 

In the above code, a request URL string of /help/detail?uuid=SomeValue will ensure that uuid is set to SomeValue. When the request param is missing and the URL request string is: /help/detail, then the uuid method argument will be set to: hello.

For more on accessing request parameters see A Footnote on Accessing Request Parameters using Spring 3 MVC.


9 comments:

blong824 said...

Do you know how to encode the uuid so it is not visible to the user for security converns?

Roger Hughes said...

Yes, the answer is to encrypt the uuid using something like RC4 and then convert that to ASCII using base64. Take a look at this blog

Unknown said...

Awesome logic, thanks and this help me for different program! Sample Contracts

aprajitha said...

Nice post. I would like to know the difference between @RequestMapping's param attribute AND @RequestParam.

Method 1:
@RequestMapping(value = "/help/detail", method = RequestMethod.GET)
public String displaySomeHelpDetail(@RequestParam("uuid") String uuid, Model model) {
return "view.name"
}


@RequestMapping(value = "/help/detail", params={"uuid"}method = RequestMethod.GET)
public String displaySomeHelpDetail(Model model) {
// How to use the "uuid" in this way?
return "view.name"
}

Roger Hughes said...

Aprajitha

A good question...

@RequestMapping is only used to map request URLs to your controller. The 'params' value is used to narrow the mapping down allowing you to map different param values (uuids in this case) to different methods. For example:

/** This is only called when uuid=6 e.g.: /help/detail?uuid=6 */
@RequestMapping(value = "/help/detail", params={"uuid=6"} method = RequestMethod.GET)
public String displaySomeHelpDetail(Model model) {

// Do Something
return "view.name"
}

/** This is only called when uuid=1234 e.g.: /help/detail?uuid=1234 */
@RequestMapping(value = "/help/detail", params={"uuid=1234"} method = RequestMethod.GET)
public String displaySomeHelpDetail(Model model) {

// Do Something Else
return "another.view.name"
}

Whilst @RequestParam is used to pass request parameters into a method so that you can use them. For example:

@RequestMapping(value = "/help/detail", method = RequestMethod.GET)
public String displaySomeHelpDetail(@RequestParam("uuid") String uuid, Model model) {

log.info("The uuid = " + uuid);
return "view.name"
}

salient1 said...

Most of your examples don't actually require you to use the @RequestParam annotation. You could have just created a String parameter called uuid and Spring would still automap it for you. You only need the annotation if you want your variable be different than the name of the value passed in or you want to change the default value, etc. Otherwise, it's a waste of space.

Roger Hughes said...

salient1
A good point. So good that I've written a quick blog outlining this technique. See A Footnote on Accessing Request Parameters using Spring 3 MVC

Unknown said...

Thank you for making it so clear. Helped me a lot.

Yogi said...

Thanks a lot for posting this....

You can visit my blog

http://javakafunda.blogspot.com

for some interesting posts :)