| This Chapter | |
| - | Chapter 4: Input Validation and Data Conversion |
| - | Data Types for Your Action Form’s Properties |
| - | Input Validation and Data Conversion Example |
| - | Using BeanUtils |
| - | Efficient Conversion |
| - | Summary |
There are two points to ponder when choosing data types for an action form’s properties.
Because request parameters are strings, populating an action form’s properties with request parameters will be easiest if each of the action form’s properties is a string. Struts does convert a request parameter value to a data type suitable for the corresponding property of an action form prior to populating the property. However, it can only convert string values to java.lang.String or Java primitive types (boolean, int, long, etc) and their Java object equivalents (Boolean, Integer, Long, etc). If you are using a java.util.Date for an action form’s property, for example, Struts will invariably throw an exception when trying to convert the corresponding request parameter to the Date property, regardless whether or not the request parameter has the correct date pattern. This is to say, you can choose String, primitives or their Java equivalents as the data types of your action form’s properties, but not other types such as java.util.Date.
What data types should you use for an action form’s properties? Here are some suggestions.
There is a dilemma here. On the one hand, choosing String as the data type of an action form’s properties makes data conversion from the HttpServletRequest object’s parameters easier. On the other hand, for easier data conversion from an action form to a transfer object, you want the action form’s properties to have compatible data types with those of the transfer object.
Let’s see how to tackle this problem in the sample applications.