Thanks Hans. I thought this may be an issue (like you say, it seems to be a
bit of a gray area): I tried taking out the overridden setters on one of the
properties once, which didn't fix the problem, but there were other
overridden setters on other properties that were likely confusing the
introspection mechanism.
The issue I have with taking out my over-ridden setters is that I haven't
found a reliable way to use <jsp:setProperty .../> to set properties
conditionally based on particular request parameters (e.g. I only want to
execute the setProperty tags when request.getAttribute("test") is non-null).
At the moment I get around this by manually calling the setxxx() methods
(which itself seems like a pretty unpleasant hack): is there a better way to
do this? I can't use a <% if... { %> <jsp:setProperty...>... <% } %> as
that causes a JSP compilation error. The setProperty code I use is:
<jsp:setProperty name="quotecalc" property="carYear"
value="<%=request.getParameter("carYear")%>" />
request.getParameter("carYear") is null when this page is called and the
if(..) evaluates to false. Thus, value is getting the empty string (well, I
presume it is -- I imagine response.out.print(null) should output nothing).
Obviously, attempting to parse an integer out of "", which the setProperty
mechanism is going to have to do, is likely to cause problems. So my
problem is, how do I best get around this? The setProperty code won't be
executed unless those parameters exist and are valid, so I could always
create a load of local variables, initialise them to junk, and just put in
the right values if they're available (then use these in the setProperty
value fields), but this just seems like a hack: the only real advantage I
get is not having to parse to the right types to call the setxxx() methods.
I guess that's quite a benefit, but it seems pointless to create all of
these local variables and then bugger about with them when the setProperty
code won't even be called unless there are valid values!
I guess what I'm asking is "Is there a better way?". If not, then I'm sure
that that will work, it just won't be especially pretty. But then, the
overridden setters isn't exactly pretty either, especially when the
overriding really just duplicates what the container can (and should)
already do...
--Chris Tucker
-----Original Message-----
From: A mailing list about Java Server Pages specification and reference
[mailto:[EMAIL PROTECTED]]On Behalf Of Hans Bergsten
Sent: Thursday, December 06, 2001 11:07 AM
To: [EMAIL PROTECTED]
Subject: Re: <jsp:getProperty() ...> problems
Chris Tucker wrote:
> [...]
> I'm probably missing something very obvious here, but it's got me
flummoxed
> right now. I'm running Tomcat 4.01, JDK 1.3.1_01, and I'm having trouble
> with exceptions when trying to read Bean properties:
>
> 2001-12-06 09:52:24 StandardWrapperValve[jsp]: Servlet.service() for
servlet
> jsp threw exception
> org.apache.jasper.JasperException: Cannot find a method to read property
> 'carYear' in a bean of type 'QuoteCalculationBean'
>
> I've been having this problem in an intermittent fashion within this
> particular Bean (not seen it in others => almost certainly my fault!) on
> various different properties. There are appropriate get/set methods for
the
> properties it can't find. In the case of carYear they are:
> int getCarYear();
> void setCarYear(int v);
> void setCarYear(String v);
> [...]
The problem is likely that you have multiple setter methods for the
property with different types. This is admittedly a gray area of the
bean spec, but discussions I've had with the bean spec leads (as well as
the implementation of the bean classes) makes it clear that this is a
no-no; a property can only have one type, hence only one setter method.
If all you want is being able to set the property to an int (using a JSP
expression) or a static string representing a number, define the property
as an int. The container takes care of the convertion from String to
int automatically. If you use JSP 1.2, you can also use PropertyEditor
to define conversions from String values to any Java type. See my JSP 1.2
article for details:
<http://www.onjava.com/pub/a/onjava/2001/11/07/jsp12.html>
Hans
--
Hans Bergsten [EMAIL PROTECTED]
Gefion Software http://www.gefionsoftware.com
Author of JavaServer Pages (O'Reilly), http://TheJSPBook.com
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST
DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:
http://archives.java.sun.com/jsp-interest.html
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.jsp
http://www.jguru.com/faq/index.jsp
http://www.jspinsider.com
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:
http://archives.java.sun.com/jsp-interest.html
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.jsp
http://www.jguru.com/faq/index.jsp
http://www.jspinsider.com