Hi Martin,
> My understanding is that this is how it's supposed to work. The idea behind
> form beans is that they represent exactly what the user entered, so that the
> same information can be used to populate the form when errors occur. A
Yes. In my case, "the same information" would mean to re-populate the optional
field with an empty string.
> struts:text value is, not surprisingly, text, so transforming it to an
I believe struts:text is named 'text' to clarify the relation to the
associated HTML-tag (in this case: <input type=text...>. This pattern
is also used for textarea, submit and so.)
> Integer does not retain all of the information required to do this. For
> example, you cannot distinguish between whether the user entered "0" or "".
Of course "0" and "" are distinguishable. Please note, that my property is
of type "Integer" and not of type int (in which case you're opinion would
be correct).
In my opinion, a non-existing field should _not_ lead to a setXXXProperty-call
(maybe checkboxes are an exception...). You would then get the following
behaviour:
-> User request form page
<- Server sends empty form page
-> User fills form page, makes some errors and leaves optional fields blank
-> User submits form
-> Struts parses the request and converts all (non-empty, non-null) parameters
to setProperty calls (*)
-> Struts detects an error and forwards to the form.jsp again
-> The form.jsp builds the form page and populates the fields with the
previously received parameters retrieved from the form bean properties
(Null properties generate value="", btw.)
<- Server sends re-populated input form again
(*) This behaviour would be consistent with the one the spec. demands
for <jsp:setProperty param="*">, (ie. to ignore both missing and empty
parameters).
To come back on the mapping of "0" and ""... I think the correct mapping
should be
FORM PROPERTY
-----+----------
"0" -> Integer(0)
"" -> null
However, what happens is
FORM PROPERTY
-----+----------
"0" -> Integer(0)
"" -> Integer(0)
which is not a unique transformation and therefore could not be reversed
when an error is detected and the form has to be repopulated.
> The right way to do this is, I believe, to have all your form bean fields be
> Strings, and do any necessary type conversions as the Action needs to access
> the values in the form bean. The form bean can (and probably should) do the
> validation, though, to ensure that the type conversions will succeed at that
> time.
Maybe I will end up with this, but I still don't think that "it's the right
way" ;)
Matthias (mailto:[EMAIL PROTECTED])