3
我試圖在我的自定義標記的屬性中傳遞整數數組。這是我迄今爲止所做的。從自定義標記屬性傳遞數組
hello.tld
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>2.1</jsp-version>
<short-name>helloTag</short-name>
<uri>/WEB-INF/lib/hello</uri>
<tag>
<name>arrayIterator</name>
<tag-class>bodyContentPkg.ArrayIterator</tag-class>
<bodycontent>JSP</bodycontent>
<attribute>
<name>array</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
ArrayIterator.java
package bodyContentPkg;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class ArrayIterator extends BodyTagSupport{
int count;
int[] myArray;
int returnValue;
public int[] getArray(){
return myArray;
}
public void setArray(int[] myArray){
this.myArray=myArray;
}
@Override
public int doStartTag() throws JspException {
count=0;
return EVAL_BODY_INCLUDE;
}
@Override
public int doAfterBody() throws JspException {
if(count==myArray.length-1){
returnValue=SKIP_BODY;
}
else{
returnValue=EVAL_BODY_AGAIN;
if(count<myArray.length){
JspWriter out = pageContext.getOut();
try {
out.println(myArray[count]);
} catch (IOException e) {
e.printStackTrace();
}
}
count++;
}
return returnValue;
}
}
bodyContent.jsp
<%@taglib prefix="cg" uri="/WEB-INF/lib/hello.tld" %>
<%pageContext.setAttribute("myArray", new int[] {1,2,3,4,5,6});%>
<cg:arrayIterator array="${myArray}"></cg:arrayIterator>
當我運行JSP文件我得到一個空白頁。不知道什麼是錯的。
另一個問題是如何直接從標籤屬性傳遞數組而不必設置pageContext的屬性並避免寫一個scriptlet?由於