2

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:

@RequestMapping(value = "/register, 
method = RequestMethod.POST, 
params = {"notify", "agreed_tos"})
public String register(/* other params like username/email */) {
    //register user
}

@RequestMapping(value = "/register, 
method = RequestMethod.POST)
public String registerMissingToS(/* other params like username/email */) {
    // return that agreed_tos checkbox wasn't checked
}

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?

2 Answers 2

3

Solution A : Little ugly, but fastest : Remove params from RequestMapping and just use request.getParameter("agreed_tos") to check this parameter.

Solution B : Use command object in form, so thymeleaf or spring tags can automatically add hidden input sent in case if user left checkbox empty and SpringMVC bind this input to command object field

Solution C : Probably @RequestParam with defaultValue and required=false allow to do both cases in one method

Sign up to request clarification or add additional context in comments.

2 Comments

I was thinking of doing C, but can you please expand on B? I haven't heard of using a command object.
Check document : Thymeleaf + Spring 3 -> chapter 6 on thymeleaf.org/documentation.html / example in this chapter is complicated, in your case it is much simpler /
1

You don't have to worry about your checkboxes not being passed when disabled. Spring MVC does that for you, and Thymeleaf provides the adequate infrastructure for it to do so. If your form-backing bean (also called command in Spring MVC jargon) has a boolean field, you can map it to a checkbox with no worries.

Whenever an input type="checkbox" is used in a Spring MVC app (be it JSP or thymeleaf), an input type="hidden" is added just after it under the hood, so that the controller layer is perfectly able to differentiate between the field being disabled or unchecked.

Disclaimer, per StackOverflow rules: I'm the author of thymeleaf.

1 Comment

I don't want to have the checkbox boolean in my Command object since it's only needed in the Controller level (if true, register account, otherwise notify user that he has to agree). Can I have a method public String doRegistration(@RequestParam("checkbox_name") Object checkboxValue, /*Command Object*/ Account account); and simply check if the checkboxValue == null? Will Spring find the hidden input generated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.