permaculture playing cards*
The moose likes Servlets and the fly likes Form Validation, Beans, and Data Types Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Form Validation, Beans, and Data Types" Watch "Form Validation, Beans, and Data Types" New topic
Author

Form Validation, Beans, and Data Types

Quinn Stone
Greenhorn

Joined: Apr 24, 2001
Posts: 2
Hi:
I hope someone has come up with an elegant method that solves my
problem, and is willing to share.
My application is based on the Chapter 6 sample in Wrox "Professional JSP 2nd Ed."):
1. Controller looks up and calls an event handler's process() method.
2. The event handler populates one or more beans using JDBC/mySQL and stuffs them in session/request as required.
3. Controller forwards to jsp, which uses beans to display stuff in a form.
Fine so far. Now, the user can update the form fields and click an "Update" button, which starts the above steps going again.
If the event handler now finds an error in the user's input, it creates an error bean and the controller forwards to the jsp again. I want the jsp to display the erroneous input as well as the error messages. However, the bean that the jsp is reading from has data types that may not accept the user's input. For example, the user might put 15.q0 (after all, the 'q' is really close to the 'one' key) in a form field that is defined as a double in the bean.
I thought, OK, just make the bean properties Strings, and I won't have to worry about it. The problem with this is that then I will have to do data type conversions whenever I want to use the bean to actually do some processing (for example, add the double value to something, do some date manipulation, etc.), which makes things, well, ugly.
I suppose I could keep one bean with nothing but String properties (string bean?) for display, and another with correct data types, but that's ugly, too, unless there's a nice way to accomplish this that I haven't thought of.
I really don't want to have two jsp's displaying the same info, or add conditions to the jsp causing it to read from the bean or the request depending on the situation.
Now that you've waded through all that, any suggestions?
Bob White
Greenhorn

Joined: Jun 23, 2001
Posts: 9
I find it best to do this kind of form validation in Javascript. It's faster for the user -- why go back to the server to discover the 'q' in a numeric field. I go back to the server only to validate input against server content.
That said, you could give your beans a dual personality. I don't know the WROX example so feel free to ignore this advice. But I assume that at some point you generate the "value" attribute of the HTML "input" tag. E.g.

Give your beans with a String field and output the value tag using a method like this:
Quinn Stone
Greenhorn

Joined: Apr 24, 2001
Posts: 2
Thanks for the reply, Bob.
Yes, there will be javascript validation, but I don't want to rely on it. Never know when someone will turn off javascript in their browser, and I want my app to handle input from other places besides browsers.
The dual identity option is there, but then not only do I need to do the extra getter processing, but also have to have extra getter methods for those times when I want to get the double value rather than a String. Sigh. There might just be no way around it.
Again, thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Form Validation, Beans, and Data Types