Apologies if this was already posted. I requested the digest as well as looked at the archives & didnt find the below message. So Im assuming it did not get posted.
> -----Original Message----- > From: Gogineni, Pratima > Sent: Monday, April 12, 2004 4:59 PM > To: '[EMAIL PROTECTED]'; '[EMAIL PROTECTED]' > Subject: Re: [OT] Overloaded setters in JavaBeans > > Hi, > > I ran into the same problems of overloaded setters in particular for > indexed properties. I have created a short Note based my observations if > it is of any help to other users. > Please comment/correct/add to the note... > > Also i think the design patterns for list based indexed properties is > very counterintuitive because: > as per Java Bean spec following design pattern necessary for indexed > properties. > <PropertyType>[] get<PropertyName>(); > public void set<PropertyName>(<PropertyType>[] value); > <PropertyType> get<PropertyName>(int index); > public void set<PropertyName>(int index, <PropertyType> value); > > BUT IF I try to create simulate an indexed property based on list > (ofcourse I havent found anything about indexed properties using LISTS in > the java spec so I am assuming this is a struts specific extension) I > would have thought the following would work but this results in the > "overloading" problem. I have to rename the indexed getter-setter pair for > the whole thing to work as I expect it to. > > List get<PropertyName>(); > public void set<PropertyName>(List value); > <PropertyType> get<PropertyName>(int index); > public void set<PropertyName>(int index, <PropertyType> value); > > > Pratima > > << File: BeanDesign.txt >> [Gogineni, Pratima] <<BeanDesign.txt>>
Bean Design The input and output beans are to be designed in such a way that they can be easily accessed through the JSP pages & manipulated through the standard tag libraries. The following should be kept in mind during bean design: (a) confirm to the java bean specification for your bean design: http://java.sun.com/products/javabeans/docs/spec.html (b) Overloading of methods that confirm to the bean specification is not supported if you plan to use the beans with the struts standard tag libraries. This is because the struts tag library uses java.beans.Introspector to find the appropriate methods & the JRE implements this as "discover a matching pair and ONLY the matching pair; i.e. no other methods by the same name". Which means overloading will cause it to not discover the property. (c) To get around the above problem if you want additional accessors rename the property. Eg: IF you have a property called name: you need an “accessor-mutator” pair: public String getName(); public void setName(String name); For the purposes of your appliction if you also needed “public void getName(String type)” – rename this to public void getNameByType(String type). (d) Java Bean spec says indexed properties should confirm to the following design pattern: <PropertyType>[] get<PropertyName>(); public void set<PropertyName>(<PropertyType>[] value); <PropertyType> get<PropertyName>(int index); public void set<PropertyName>(int index, <PropertyType> value); (e) Note that (d) above refers to java arrays. For access through tags we can also have “List based indexed properties”. There are 3 ways of accessing these as shown in the example below. Assume you have a property “commands” in a bean “AFWrapper”. The commands property is indexed & each object is of type AFServerCommand. AFServerCommand has a property interfaceMethodName. · As per Struts manual (http://jakarta.apache.org/struts/faqs/indexedprops.html). if we define the following method: public ArrayList getCommands(int index){ return commands; } This will cause Struts to discover the commands indexed property and the allow the following usage: <bean:write name="AFWrapper" property="commands[0]"/> How ever the following will FAIL! because struts does not know of an indexed getter of the commands property. <bean:write name="AFWrapper" property="commands[0].interfaceMethodName"/> · To get the above tag that failed to work Define the following method: public AFServerCommand getCommands(int index){ return (AFServerCommand)commands.get(i); } Then the tag that failed above <bean:write name="AFWrapper" property="commands[0].interfaceMethodName"/>. How ever the following will FAIL!: <logic:iterate id="cmdObj" name="AFWrapper" property="commands"> InterfaceMethodName: <bean:write name="cmdObj" property="interfaceMethodName"/> <br/> </logic:iterate> · To get the above iterate tag to work do the following: Change public ArrayList getCommands(int index){ return commands; } To: public ArrayList getCommands(){ return commands; } Now the property is not discovered because of the “overloading” problem that we have described in (b) above. So rename public AFServerCommand getCommands(int index){ return (AFServerCommand)commands.get(i); } To public AFServerCommand getCommand(int index){ return (AFServerCommand)commands.get(i); } Now both the following tags will work: <logic:iterate id="cmdObj" name="AFWrapper" property="commands"> InterfaceMethodName: <bean:write name="cmdObj" property="interfaceMethodName"/> </logic:iterate> Interface methodName for cmd[0]: <bean:write name="AFWrapper" property="command[0].interfaceMethodName"/>
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]