• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

using primitives in spring binding

 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the best way to bind an input form on a web page to a primitive type in an object? Say you have an input form where someone types in an age, and that age is stored as an int in my object. If I type a value in the form field, it binds correctly to the int field in my object.

However, if I leave my form field blank and press submit, it complains that it cannot convert string to int.

Is the way to handle this by providing a custom property editor, or is the preferred way to manually get that form parameter from the HttpServletRequest and populate the object?

Thanks for the help
Brian
 
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Brian,

Typically, you would associate an implementaion of the Spring Validator interface with your controller which you can then use to provide a more appropriate error message. Take a look at the Petclinic sample app for an example.
 
Brian Nice
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I ended up writing a custom property editor that seems to do the job. It is specific for int fields, it would be nice to extend it to handle any type of primitive. Thanks for the reply
Brian
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic