4

I am having an issue with the Spring form:checkbox tag.

I currently have a JSP page with a form:checkbox tag bound to a Java boolean field. When I put a tick in the checkbox and submit the form the value is false.

Here is the checkbox on my JSP:

<form:checkbox id="field_termsandconditions" path="agreeTermsAndConditions" />

My GET controller:

@RequestMapping(value = "/page1.htm", method = RequestMethod.GET)
public String getPage(HttpServletRequest request, ModelMap model) {
    model.addAttribute("MyObject", new MyObject());
    return getURL(request);
}

My POST controller:

@RequestMapping(value = "/page1.htm", method = RequestMethod.POST)
public String processPage(HttpServletRequest request,
    HttpServletResponse response,
    ModelMap model,
    MyObject myObject,
    BindingResult bindingResult) {

        System.out.println(myObject.isAgreeTermsAndConditions);
}

myObject.isAgreeTermsAndConditions is always false when it hits the POST controllers even when checked!

Any ideas?

1
  • Don't know how spring-mvc works, but in HTML checkboxes have string values of ON or OFF, so you need a mapping from the string values to boolean. Commented Jan 21, 2014 at 22:28

1 Answer 1

1

This might be a little late to answer, but maybe it will help some other person.

When you auto-generate getter and setters for boolean values it is very often generated without 'is' prefix.

For instance, in the case mentioned above the generated setter for 'isAgreeTermsAndConditions' property might be the following: 'setAgreeTermsAndConditions()', note there is no 'is' prefix in the method. The same true for the getters as well.

Since property getter and setters names are used find and bind to the model property, the checkbox might be not shown on the UI or not working properly if there are property name and getters/setters mismatch.

Make sure the property 'isAgreeTermsAndConditions' has the following getters/setters method names: getIsAgreeTermsAndConditions()/setIsAgreeTermsAndConditions(...)

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

Comments

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.