struts-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "enzhong fu" <enzhon...@adelphia.net>
Subject automatically handle invalid input format
Date Fri, 27 Feb 2004 14:42:54 GMT
Currently, Struts recommends all fields of ActionForm be of type String. If you use other type
(such as Date), and user inputs the value in an invalid format so the value could not convert
to the suitable type, BeanUtils would throw an exception and it would propagate back to the
user. Use only String type for ActionForm fields can display to the user the original values
they input, but in your Action class code is needed to transform from String type to type
used in business objects.

Not all people like this. Today one more developer posted it as a bug. Ideally, we hope to
use more types in a form bean while still able to display the original inputs to the user
when a validation error occurs. I think the following solution can achieve it,

0. Basic validation, "Valid Format" and "Required", should be handled during populating form
bean.

1. In RequestProcessor class, processPopulate() should mimic processValidate(). It returns
a boolean to indicate whether processPopulate is successful or not. If not successful, it
should forward to the input page like processValidate does.

2. Instead of throwing exception, BeanUtils class should catch the exception for populating
error and continue. It returns a list of the fields that have problem (normally invalid format).
Error messages constructed based on these fields will be stored in Globals.ERROR_KEY.

3. When there is populating error, a HashMap is created to hold the original request parameters
(all Strings, nested fields can have nested HashMaps). The HashMap is stored at a global key
(Globals.xxx) in the request.

4. The html:form tag will get the stored HashMap and put it to Constants.BEAN_KEY for field
tags to use (when there is populating error).

Thus, you can use other types in form bean (nest business objects in form bean, for example)
and Struts can still display the user's original input when in error. I tried it on Struts1.1-rc2
and it worked. I did not try complex cases such as array type and nested fields but I think
it will work. What's your opinion? I began to look at Struts source code only recently and
I'm still not quite familiar with the code. Comments from you people on whether it's worthwhile
to implement it in Struts and the best way to do it if we decide to move on would be very
helpful.

Andrew

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message