Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What type should Struts ActionForm properties be?

I inherited this gigantic legacy Java web app using Struts 1.2.4. I have a specific question regarding ActionForms. Some of them have only String properties (even for numbers), some of them use the seemingly appropriate types (Integer, Date, String, etc). What's the best practice here?

Also, it seems that if a property is of type Integer, and the value the user entered is not an integer value, Struts silently swallows this and just doesn't set the property. What's up with this?

like image 564
thvo Avatar asked Oct 16 '08 17:10

thvo


People also ask

What is ActionForm in struts?

An ActionForm is a JavaBean optionally associated with one or more ActionMappings. Such a Bean will have had its properties initialized from the corresponding request parameters before the corresponding action's execute() method is called.

What is form bean tag in struts config xml?

The bean tag is a combination of the set and push tags, it allows you create a new instance of an object and then set the values of the variables. It then makes the bean available in the valuestack, so that it can be used in the JSP page.


2 Answers

For Struts 1.2x, properties where the user enters or selects values on the HTML form and that are populated on the ActionForm instance automatically by the Struts HTML tags must be Strings or booleans. However, there's no reason why your ActionForm can't have getter methods that return the properties as more meaningful types for your application e.g. Date.

like image 199
John Topley Avatar answered Sep 20 '22 16:09

John Topley


If you want to do some validation yourself (or use the struts validator features that have been refactored out of struts later than 1.2) you'll need String properties for exactly the reason that you cite: Once you store numerical values in Integer or int values, they must be either numbers or "nothing at all" - otherwise: how'd you store "a lot" (literally) in an Integer property?

Client Side Validation (e.g. in Javascript) would still work, but you don't want to rely on this.

If - upon entering an invalid number - you are fine with the user being presented a blank field, you may store values in Date-, Integer-, Whatever-Properties. If you'd like to present the original value with the error message, you'll need the String property.

This has the price that you need to do the conversion manually. As Struts 1.2 is quite old you might want to think about gradually replacing your application infrastructure with more modern technology and operate on two technologies (the old & the new) at the same time for a while until the old part gets small enough (or unimportant enough) to be dropped.

nb - you also get more control about what values you want to accept as numbers. I still remember an old Struts creditcard-banking-application in which I asked my manager what the expected outcome is when you'd like to withdraw 3e2 Euro from your card. They didn't say 300 (as Struts converted) but opted for an error.

like image 40
Olaf Kock Avatar answered Sep 19 '22 16:09

Olaf Kock