I'm looking through some Spring 3 MVC controller code and I see that @RequestParam is used for some parameters and not for others. Example where it is not being used:
@RequestMapping(value = "/experiments", method = RequestMethod.GET)
public String getExperimentsPage(ExperimentSearchCriteria criteria, Map<String, Object> model) {
// method body here
}
When is @RequestParam (or similar parameter-specifying annotation) not needed?
Good question, I have been wondering this too until I found it being mentioned in the doc:
Note that use of @RequestParam is optional, e.g. to set its attributes. By default any argument that is a simple value type, as determined by BeanUtils#isSimpleProperty, and is not resolved by any other argument resolver, is treated as if it was annotated with @RequestParam.
https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-ann-requestparam
Most of time, I don't specify this unless my method parameter name is different with request parameter, or, if the value is optional, I will need to use required=false
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With