Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Struts Form Bean

 
dnyan ginde
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it necessary that the properties of a form bean be of the type String? What happens if I use integer or float or for that matter anything other than String? I tried using integer and it worked fine but Ive read in quite a few books and articles that the properties of a form bean must be String. What problems can i face if i use anything other than String? Is there an explanatory answer to "why use String"?
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The biggest problem with using non-Strings in an ActionForm is with validation. Let's say you have an html:text tag on your JSP and the property is an integer. What happens if the user enters something other than a valid integer in that field? When Struts executes the setter for that field, it will throw a number Format exception. Even if you have server-side validation logic to check the field, When Struts redisplays the page, the field will show zero instead of the invalid data the user actually entered.

If you have a field that is read-only, then I see no problem with declaring it as a non-String in the ActionForm. You can even format it using the format property of bean:write.
 
dnyan ginde
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Merrill...
 
Vikrama Sanjeeva
Ranch Hand
Posts: 760
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Good info Merrill.

Bye,
Viki.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic