Chris Tucker wrote:
>
> 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.
Very likely ;-)
> 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).
I assume you mean request.getParameter(), not getAttribute().
The <jsp:setProperty> action works like this if you don't use the
value attribute:
* If request.getParameter("propertyName") returns null or an empty string,
no attempt is made to set the corresponding property,
* If the parameter value is a non-empty string and the bean's property
type is not a String, the container converts it to the appropriate
type, if possible (fixed set of supported types in JSP 1.1, may be
extended with property editors in JSP 1.2)
So I'm not sure what the issue you describe is. It seems like the behavior
of <jsp:setProperty> is exactly what you want.
> 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.
Like I said, you shouldn't have to use an if statement (unless I missed
something), but doing so should not cause a compilation error. This
is perfectly legal (even though I don't recommend it):
<% if (request.getParameter("foo") != null) { %>
<jsp:setProperty ... />
<% } %>
> 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?
You lost me here :-) When you explicitly specify a value as a JSP expression,
the <jsp:setProperty> action does not try to convert the value; if the property
is of type int, the expression must result in an int. In your example the
result is a String (or null), so it will fail. If you just remove the value
attribute and let the <jsp:setProperty> action figure things out for itself,
it should work fine.
> 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!
Sorry, but you lost me again.
> 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...
I hope the above helps, basically just let <jsp:setProperty> take care
of the problem for you.
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