Pierre Métras wrote:

> Craig McClanahan wrote:
>
> > It might make sense to skip calling the setIntProperty() field at all if
> the
> > input is a zero-length string.  JSP follows this rule when you specify
> > something like <jsp:setProperty name="beanname" property="*"/>, so this
> > would be more consistent with JSP as well.
> >
> > What do you think?
>
> It might create side effects, like with checkboxes where a non checked
> button is not sent to the server.
> Imagine that the user has entered a value in a field, but that field must be
> empty for the form to be validated. When struts redisplays the form with the
> errors, the user empties the field. With your proposal, struts will never
> transmit the empty field to the bean and the validation will always fail...
>

One of the things that is being added to the 1.0 development tree is a reset()
method on ActionForm.  This method is called *before* Struts does the
auto-populate trick.  The contract is that this method should reset all relevant
attributes for this particular form to default values.

For checkboxes, you would want to set the corresponding boolean to false.  It
will be set to true by the auto-populate method *if and only if* the checkbox
was checked (and therefore included in the POST).

For integers, it is technically feasible to distinguish an empty field ("")
versus a zero ("0").  If we decide not to call the setter in this scenario, then
the ActionForm property will remain at it's default value, so you can
distinguish whether it was entered or not.

On the other hand, we could take the approach that the form should have
displayed the default value in the first place, and the user would have had to
erase the existing value to cause an empty string to be posted -- and that the
bean should want to know about that.  Because we have to generate a legal
integer value, the decision is what value to use for indicating "no input".
Zero is the current mechanism for this, but it's not the only choice.


>
> In that particular case, the "" <==> 0 translation creates the whole
> problem. The ActionForm bean should really use String in order to
> distinguish between empty and 0 value. Then, after validation (the
> validation could have to convert the field value in order to check its
> content), the conversion should occur when the definitive values are
> transfered to the business object.
>
> Pierre Métras

Craig McClanahan

====================
See you at ApacheCon Europe <http://www.apachecon.com>!
Session VS01 (23-Oct 13h00-17h00):  Sun Technical Briefing
Session T06  (24-Oct 14h00-15h00):  Migrating Apache JServ
                                    Applications to Tomcat




Reply via email to