You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by Anthony Martin <an...@trams.com> on 2001/04/21 06:31:25 UTC

REQUEST: overloaded setters

I wonder if it is reasonable for setters to be overloaded.  I am facing the requirement for true Date and int values to be pre-populated in a ActionForm.  I know how to pre-populate, and would like to do so by using native Date and int values rather than converting them to strings first.  I have found that if I overload the setters, I get ServletExceptions.  These exceptions say that there are no getter methods defined for the properties when getter methods exist.

At this time, I have created a workaround: setXxxAsInt() or setXxxAsDate().  Am I to understand that overloading is not part of the general Sun Microsystems bean specification, and that my approach is correct?


Anthony

"When you don't know what you're doing, do it neatly."

Re: REQUEST: overloaded setters

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Fri, 20 Apr 2001, Anthony Martin wrote:

> I wonder if it is reasonable for setters to be overloaded.  I am
> facing the requirement for true Date and int values to be
> pre-populated in a ActionForm.  I know how to pre-populate, and would
> like to do so by using native Date and int values rather than
> converting them to strings first.  I have found that if I overload the
> setters, I get ServletExceptions.  These exceptions say that there are
> no getter methods defined for the properties when getter methods
> exist.
> 
> At this time, I have created a workaround: setXxxAsInt() or
> setXxxAsDate().  Am I to understand that overloading is not part of
> the general Sun Microsystems bean specification, and that my approach
> is correct?
> 

More precisely, overloading the setters is prohibited by the JavaBeans
spec, and will cause the standard Java introspection logic (which Struts
relies on) to not recognize a property by that name.

In an ActionForm, I would suggest that you use String values for these
properties.  Consider what would happen if the user types "1a23" for a
field that matches an integer property -- this will cause an exception on
the call to the setter.  More seriously, it will not be possible to
reproduce what the user last typed when the input page is redisplayed,
which violates one of the primary purposes for an ActionForm to exist.

> 
> Anthony
> 
> "When you don't know what you're doing, do it neatly."
> 

Craig McClanahan