我正在尝试在自定义标记的属性中传递整数数组。这是我到目前为止所做的。
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文件时,我得到一个空白页面。不知道什么是错的。
另一个问题是如何直接从tag属性传递数组而不必设置pageContext的属性并避免编写scriptlet?感谢
答案 0 :(得分:2)
when i run the jsp file i get a blank page. dont know whats wrong.
您使用的标签没有任何正文内容,因此未调用doAfterBody()
。将标记更改为以下内容:
<cg:arrayIterator array="${myArray}"> </cg:arrayIterator>