Oliver Suciu wrote:
> Hi,
>
> Can anybody help clarify how a JSP container should handle
> overloaded setters in a custom tag:
>
> public void setData(List dataList) {
> this.dataList = dataList;
> }
> public void setData(Object dataObj) {
> this.dataObj = dataObj;
> }
> public void setData(String dataStr) {
> this.dataStr = dataStr;
> }
>
> We want to give the JSP author the convenience of passing data
> to a tag using different types, whichever comes in more handy.
> The data would be of the same "logical" kind, that's why, ideally,
> the setters would bear the same name, just like overloaded methods
> in Java.
>
> As an example, JRun 3.1 accepts the code above, whereas Tomcat
> 3.2.3 throws a CompileException ("Unable to convert a String
> to java.util.List").
>
> What's the correct behavior?
Tomcat's behavior is correct. When it comes to attribute setter
methods, a tag handler class is a "bean", so you must go to the
bean spec to see how it deals with property setter methods.
The spec text itself doesn't say anything about multiple setter
methods, but the implementation of the methods that deal with
setter methods only allow one setter method per property. Hence,
multiple setter methods for a tag handler attribute is not
allowed.
In JSP 1.2 you can get around this by making the type of the
attribute Object, and the figure out which specific type you
got. The reason this only works well with JSP 1.2 is that
JSP 1.1 doesn't accept a string literal as the attribute
value for an attribute of type Object; JSP 1.2 does.
public void setData(Object data) {
this.data = data;
}
public int doStartTag() {
if (data instanceof List) {
// Work with it as a List
}
else (data.getClass().isArray()) {
// Work with it as an array
}
else (data instanceof String) {
// Work with as a String
}
else ...
}
I describe this in more detail in an article that will soon
be published at the O'Reilly site. I'll post the URL here
when it's available.
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://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