Is it good to use non-String type for ActionForm Property?
Alec Lee
Ranch Hand
Joined: Jan 28, 2004
Posts: 568
posted
0
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
posted
0
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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.
subject: Is it good to use non-String type for ActionForm Property?