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
-----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
===========================================================================
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