inline
-----Original Message-----
From: E Stones [mailto:[EMAIL PROTECTED]]
Sent: den 8 oktober 2001 14:01
To: Orion-Interest
Subject: Problem using custom tags.
The following example is taken from Chapter 2 of "Advanced Java Server
Pages"
by David M. Geary
I'm getting this error when I try to implement to code sample below:
--
500 Internal Server Error:
Error parsing JSP page /test1.jsp line 13
Bean 'item' already defined
--
Has anyone any ideas what might be the problem?
***************
"test.jsp" code:
***************
<html><head><title>An Iterator</title></head>
<%@ taglib uri='/WEB-INF/tlds/iterator.tld' prefix='it' %>
<body>
<% java.util.Vector vector = new java.util.Vector();
vector.addElement("one"); vector.addElement("two");
vector.addElement("three"); vector.addElement("four");
%>
Iterating over <%= vector %> ...<p>
<--- declaration of item
<it:iterate id='item' collection='<%= vector %>'>
<--- declaration 2 of item
<!--<jsp:useBean id='item' scope='page' class='java.lang.String'/>-->
//above should not be necessary.
Item: <%= item %><br>
</it:iterate>
</p>
</body>
</html>
************************
Iterator.tld
************************
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library
1.1//EN" "web-jsptaglib_1_1.dtd">
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.1</jspversion>
<shortname>smp</shortname>
<info>Sun Microsystems Press Tag Library</info>
<tag>
<name>iterate</name>
<tagclass>tags.IteratorTag</tagclass>
<teiclass>tags.IteratorTagInfo</teiclass>
<bodycontent>JSP</bodycontent>
<attribute>
<name>id</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>collection</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<info>Iterates over a collection</info>
</tag>
</taglib>
***************
IteratorTag.java
***************
import java.util.Collection;
import java.util.Iterator;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class IteratorTag extends BodyTagSupport {
private Collection collection;
private Iterator iterator;
public void setCollection(Collection collection) {
this.collection = collection;
}
public int doStartTag() throws JspException {
return collection.size() > 0 ? EVAL_BODY_TAG : SKIP_BODY;
}
public void doInitBody() throws JspException {
iterator = collection.iterator();
pageContext.setAttribute(getId(), iterator.next());
}
public int doAfterBody() throws JspException {
if(iterator.hasNext()) {
pageContext.setAttribute(getId(), iterator.next());
return EVAL_BODY_TAG;
}
else {
try {
getBodyContent().writeOut(getPreviousOut());
}
catch(java.io.IOException e) {
throw new JspException(e.getMessage());
}
return SKIP_BODY;
}
}
}
*********************
IteratorTagInfo.java
*********************
package tags;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
public class IteratorTagInfo extends TagExtraInfo {
public VariableInfo[] getVariableInfo(TagData data) {
return new VariableInfo[] {
new VariableInfo(data.getId(), // scripting var's name
"java.lang.Object", // variable's type
true, // whether variable is created
VariableInfo.NESTED) // scope
};
}
}
_________________________________________________________________
Get your FREE download of MSN Explorer at
http://explorer.msn.com/intl.asp