• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem with CheckBoxes

 
Ranch Hand
Posts: 277
Oracle Spring Flex
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Oracle Spring Flex
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can Anyone help me with this.
 
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Oracle Spring Flex
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic