I have a method in Spring controller like this;
@RequestMapping(value = "/contact", method = RequestMethod.GET)
public final ModelAndView contact(@RequestParam(value = "id", required = false) int i) {..}
I pass in the parameter i to another method. In that method, I want to check if there is a value in int i. If the parameter was not set (this is optional), I want to set it to a certain value. How do I check this in Spring?
Use Integer
instead of int
. If it's not set, it will be null.
If, for example, 0 is not a valid ID value, you can also leave the type as int
and use
@RequestParam(value = "id", defaultValue = "0")
The parameter will than be equal to 0 if not passed.
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