File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Spring and the fly likes Problem with CheckBoxes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Problem with CheckBoxes" Watch "Problem with CheckBoxes" New topic

Problem with CheckBoxes

Ashwin Sridhar
Ranch Hand

Joined: Jul 09, 2011
Posts: 272


I am trying to use Checkboxes tag. I am trying to dynamically generate checkboxes based on the String[] in the model.

userForm.jsp is my first page, I set values for a set of checkboxes which are mapped to a String[] and the action for it is "/addUser". From here I sent the Model to userNext.jsp page.

And my Model is

I get a IllegalArguement Exception stating that items should not be null.

But the String[] is not empty. Could someone help me understand this.

Ashwin Sridhar
Ashwin Sridhar
Ranch Hand

Joined: Jul 09, 2011
Posts: 272

Can Anyone help me with this.
Bill Gorder

Joined: Mar 07, 2010
Posts: 1682

I think you are misunderstanding this usage of the checkbox tag. You are getting that exception because you don't have an array or collection model attribute named hobbies.

Here is an example:

Take a simple domain object called user

Now the Controller

And finally the jsp

If you run this code you will see a page with 3 checkboxes one for each hobby and a submit button. The checkboxes are populated by the hobbyList modelAttribute. Notice I pulled this out into its own method. I could have populated this in the get method as well, however I would have had to change its type from List to ArrayList in the parameter list as Spring would otherwise try to instantiate an interface. I like coding to interfaces so I showed you the other approach.

The User object's hobbies attribute is bound to this checkbox form element so when you click submit if you set a break point in the userSubmit method you will see that the hobbies String[] on the User object will contain whatever values were checked on the JSP. After that the program will bomb as I am returning "someOtherView" which is of course not defined but you get the idea.

Hopefully that clears things up for you.

[How To Ask Questions][Read before you PM me]
Ashwin Sridhar
Ranch Hand

Joined: Jul 09, 2011
Posts: 272

Hi Bill,

Thanks for the reply. I missed out on setting the ModelAttribute.

It works now. Thank you.

I agree. Here's the link:
subject: Problem with CheckBoxes
jQuery in Action, 3rd edition