Saturday, October 12, 2013

What is the difference between @RequestMapping's param attribute AND @RequestParam

Source : http://www.captaindebug.com/2011/07/accessing-request-parameters-using.html#uds-search-results

@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" 
}