Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从自定义标记属性传递数组_Java_Jsp_Jakarta Ee_Jsp Tags_Taglib - Fatal编程技术网

Java 从自定义标记属性传递数组

Java 从自定义标记属性传递数组,java,jsp,jakarta-ee,jsp-tags,taglib,Java,Jsp,Jakarta Ee,Jsp Tags,Taglib,我试图在自定义标记的属性中传递一个整数数组。这是我到目前为止所做的 你好,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>

我试图在自定义标记的属性中传递一个整数数组。这是我到目前为止所做的

你好,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>

1
2.1
海洛塔格
/WEB-INF/lib/hello
阵列致畸器
bodyContentPkg.ArrayIterator
JSP
排列
真的
真的
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;
        }
    }
package bodyContentPkg;
导入java.io.IOException;
导入javax.servlet.jsp.JspException;
导入javax.servlet.jsp.JspWriter;
导入javax.servlet.jsp.tagext.BodyTagSupport;
公共类ArrayIterator扩展了BodyTag支持{
整数计数;
int[]myArray;
返回值;
public int[]getArray(){
返回myArray;
}
公共void setArray(int[]myArray){
this.myArray=myArray;
}
@凌驾
public int doStartTag()抛出JSPEException{
计数=0;
返回评估主体,包括:;
}
@凌驾
public int doAfterBody()抛出JSPEException{
if(count==myArray.length-1){
returnValue=跳过正文;
}
否则{
returnValue=EVAL\u BODY\u;

如果(count
当我运行jsp文件时,我得到一个空白页。不知道出了什么问题。

您使用的标记没有任何正文内容,因此不会调用
doAfterBody()
。请将标记更改为类似以下内容:

 ;

<%@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>