Handling and validating HTML checkbox with Spring MVC controller

alpha98

New Member
I'm using Spring MVC and I want a page to register my users. I want to give them the option to receive email notification and I have some ToS they have to accept, so two checkboxes. I'm restricted with HTML checkboxes because I'm also using thymeleaf as my templating engine. I want to know how you would handle and validate the value of the checkbox, whether they are checked or not. When you do a form submit with checkboxes, if they are checked, the value is passed to the server as name-value pair of checkbox, otherwise nothing is passed. Do you have different handler methods like below:\[code\]@RequestMapping(value = "http://stackoverflow.com/register, method = RequestMethod.POST, params = {"notify", "agreed_tos"})public String register(/* other params like username/email */) { //register user}@RequestMapping(value = "http://stackoverflow.com/register, method = RequestMethod.POST)public String registerMissingToS(/* other params like username/email */) { // return that agreed_tos checkbox wasn't checked}\[/code\]In this way, the first method will get called if the checkbox is checked and the second if it isn't. Is there a way to check if a checkbox is checked so I can do all this in a single controller method? How would you do this?
 
Back
Top