Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Struts and the fly likes Is it good to use non-String type for ActionForm Property? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Is it good to use non-String type for ActionForm Property?" Watch "Is it good to use non-String type for ActionForm Property?" New topic
Author

Is it good to use non-String type for ActionForm Property?

Alec Lee
Ranch Hand

Joined: Jan 28, 2004
Posts: 569
I am using Struts. And, I need to gather some dollar value from text box. I prefer to use java.math.BigDecimal to represent the value in backend. Should I define the ActionForm property of type java.math.BigDecimal directly instead of String?

I know I can do this and it seems that struts can convert input parameter (String) into BigDecimal automatically. Furthermore, I can also use BigDecimal type in DynaActionForm and the similar conversion is done automatically.

But, the problem is if I input some non-numeric value in the text box which is incompatible with BigDecimal's constructor, struts throws an exception in the browser! Is there any way to catch the exception? Or, is it a good design to use BigDecimal at all?

Afterall DynaActionForm does support it and a bunch of other non-String type, so I guess there is some good reasons behind this. But, If it is a good design, there would be another problem. How can the ActionForm store the invalid input value (some non numeric character) in BigDecimal and redisplay it in the response?
John Smith
Ranch Hand

Joined: Sep 02, 2004
Posts: 61
I find it easier to use String properties only in the ActionForms and convert back and forth to numbers in my Action classes if I need to, it makes things easier in the jsp. I think the struts tags will try to convert anything to a String anyway but if you do it yourself you can guarantee it will be done and can handle removing any excess decimal places you don't want and such
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is it good to use non-String type for ActionForm Property?