This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Other Application Frameworks and the fly likes using primitives in spring binding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "using primitives in spring binding" Watch "using primitives in spring binding" New topic
Author

using primitives in spring binding

Brian Nice
Ranch Hand

Joined: Nov 02, 2000
Posts: 195
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
Ken Krebs
Ranch Hand

Joined: Nov 27, 2002
Posts: 451
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.


kktec<br />SCJP, SCWCD, SCJD<br />"What we observe is not nature itself, but nature exposed to our method of questioning." - Werner Heisenberg
Brian Nice
Ranch Hand

Joined: Nov 02, 2000
Posts: 195
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
 
Don't get me started about those stupid light bulbs.
 
subject: using primitives in spring binding
 
Similar Threads
Best practice to get table form data back to Java beans
Error while validating the input
Struts2 type conversion error message
form field's value from a string
struts integer validator