Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with CheckBoxes

 
Ashwin Sridhar
Ranch Hand
Posts: 277
Flex Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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
Ranch Hand
Posts: 277
Flex Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can Anyone help me with this.
 
Bill Gorder
Bartender
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

User.java




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.
 
Ashwin Sridhar
Ranch Hand
Posts: 277
Flex Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bill,

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

It works now. Thank you.

Regards,
Ashwin
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic