Hans you are right, I've never tried method overloading on property setters
or getters before. Checking my first code sample with JBuilders BeanInsight
Wizard, showed only one property: an age property of type String.
Thinking about it, it seems logical to have just one getter/setter for each
property. For example declaring the methods: int getNumber() and
setNumber(int), defines a property 'number' of type integer, even if no
private field 'number' exists. However if there already is a field 'number'
of type String with setter/getter methods (or just the String type setter
and getter methods) there is a problem: the bean would have two properties
'number', one of type String and one of type int.
In his original question Aaron seemed to prefer the age property as integer,
I think changing the name of the setAge(String) method should do the trick:
public class MyBean {
private int age = -1;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void setAgeFromString(String ageParam) {
try {
setAge(Integer.valueOf(ageParam).intValue());
}
catch (NumberFormatException nfe) {
// Handle the exception the way you want
}
}
}
Nick
-----Original Message-----
From: A mailing list about Java Server Pages specification and reference
[mailto:[EMAIL PROTECTED]]On Behalf Of Hans Bergsten
Sent: 6-Oct-00 3:00 AM
To: [EMAIL PROTECTED]
Subject: Re: types of parameters from JSPs
Nick Ouwehand wrote:
>
> JavaServer Pages 1.1 Specification states the following:
>
> >If you set propertyName to * then the tag will iterate over the
> >current ServletRequest parameters, matching parameter names
> >and value type(s) to property names and setter method type(s), setting
> >each matched property to the value of the matching parameter. If a
> >parameter has a value of ��, the corresponding property is not
> >modified.
>
> Note the matching of value type(s) and setter method type(s).
> I think your original code needs the setAge(int) method to get a valid
bean.
> Adopting the code Hans has send, something like this should work:
>
> public class MyBean {
> private int age = -1;
>
> public int getAge() {
> return age;
> }
> public void setAge(int age) {
> this.age = age;
> }
> public void setAge(String ageParam) {
> try {
> setAge(Integer.valueOf(ageParam).intValue());
> }
> catch (NumberFormatException nfe) {
> // Handle the exception the way you want
> }
> }
> }
>
> Nick
No, you can't have two setter methods with different argument types for
the same property, so with the above code the "age" property would not
be recognized; you have setAge() with an int and a String argument.
The JavaBeans spec is a bit vague about this, but run some tests with JDK
1.3
and you'll see that it only works if you have one and only one setter method
for a property.
Hans
> -----Original Message-----
> From: A mailing list about Java Server Pages specification and reference
> [mailto:[EMAIL PROTECTED]]On Behalf Of Hans Bergsten
> Sent: 5-Oct-00 9:00 PM
> To: [EMAIL PROTECTED]
> Subject: Re: types of parameters from JSPs
>
> Aaron Naiman wrote:
> >
> > Dear All,
> >
> > Hi. Thank you Stephanie and Hans and some others for answering. The
> > following seems to be the upshot, and I would not mind if some of you
> > gurus out there would confirm:
> >
> > 1) The container (basically) requires that the type of argument of
> > setFoo() must match the type of the return value of getFoo().
> >
> > 2) jsp:setProperty will always send a String, and then conversions are
> > done as appropriate for the argument of each setFoo().
> >
> > 3) If setFoo() expects an int, and a "b" was sent instead of "7", I
> > cannot catch this exception. (Except maybe put a scriptlet with a
> > try() around all of my jsp:setProperty calls, which would be ugly,
> > IMHO.)
> >
> > 4) Therefore setFoo() has to receive a String, and I have to use
> > Integer.parseInt(), etc.
> >
> > 5) Therefore getFoo() also has to return a String (because of 1)
> > above).
> >
> > 6) Therefore, in order to be able to catch such exceptions--which of
> > course should be done, really the only type of variables which can
> > be processed in a bean from a JSP is a String (and then doing all
> > of the conversions). This is contrary to all of the material one
> > reads on the matter.
> >
> > Is this how other people handle this issue?
> >
> > Thanx for your thoughts,
>
> Your summary sounds right; since HTTP parameters are strings, you need
> your bean property to be of type String as well in order to take care
> of validation and conversion (e.g. to an int) yourself. But if you need
> the value as an int somewhere else, you can add another getter method
> to the bean:
>
> public class MyBean {
> private String ageParam;
> private int age = -1;
>
> public void setAgeParam(String ageParam) {
> this.ageParam = ageParam;
> try {
> age = Integer.valueOf(ageParam).intValue();
> }
> catch (NumberFormatException nfe) {
> // Handle the exception the way you want
> }
> }
> public String getAgeParam() {
> return ageParam;
> }
> public int getAge() {
> return age;
> }
> }
>
> If you never want to get the value as a String, just remove the
> getAgeParam()
> method; it's valid to have a property with just a getter or a setter
method.
>
> Hans
> --
> Hans Bergsten [EMAIL PROTECTED]
> Gefion Software http://www.gefionsoftware.com
--
Hans Bergsten [EMAIL PROTECTED]
Gefion Software http://www.gefionsoftware.com
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html
http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html
http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets