2

我创建了一个自定义标签,但是当我使用以下异常时:-

An error occurred at line: 14 in the jsp file: /index.jsp
  LoopTextTag cannot be resolved to a type
   <body>
     <center>
         <ct:loopText times="5">
            <h3>Loop Text!</h3>
        </ct:loopText>

堆栈跟踪:

Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:92)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:423)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:308)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:286)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:273)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:566)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:317)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

我的 LoopTaxtTag 类如下:

 import java.io.IOException;
 import javax.servlet.jsp.JspException;
 import javax.servlet.jsp.tagext.BodyContent;
 import javax.servlet.jsp.tagext.BodyTagSupport;


public class LoopTextTag extends BodyTagSupport {


private static final long serialVersionUID = 1L;
private int mTimes = 0;
private BodyContent mBodyContent;

public void setTimes(int pTimes) {
    mTimes = pTimes;
}


public void setBodyContent(BodyContent pBodyContent) {
    mBodyContent = pBodyContent;
}


@SuppressWarnings("deprecation")
public int doStartTag() throws JspException {
    if (mTimes > 1) {
        return EVAL_BODY_TAG;
    } else {
        return SKIP_BODY;
    }
}


@SuppressWarnings("deprecation")
public int doAfterBody() throws JspException {
    if (mTimes > 1) {
        mTimes--;
        return EVAL_BODY_TAG;
    } else {
        return SKIP_BODY;
    }
}


public int doEndTag() throws JspException {
    try {
        if (mBodyContent != null) {
            mBodyContent.writeOut(mBodyContent.getEnclosingWriter());
        }
    } catch (IOException pIOEx) {
        throw new JspException("Error: " + pIOEx.getMessage());
    }
    return EVAL_PAGE;
}
}

我的 index.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib  prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@page import="com.pt.LoopTextTag"%>
<%@ taglib uri="/WEB-INF/custom.tld" prefix="ct" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
< %@ taglib uri="/WEB-INF/custom.tld" prefix="ct" %>

</head>
<body>
    <center>

        <ct:loopText times="5">
            <h3>Loop Text!</h3>
        </ct:loopText>

    </center>
</body>

编译器也提出了一个警告:

The tag handler class for "ct:loopText" (LoopTextTag) was not found on the Java Build Path
4

1 回答 1

0

如果您还没有弄清楚,问题出index.jsp在以下行:

 < %@ taglib uri="/WEB-INF/custom.tld" prefix="ct" %>

uri这里应该与文件<uri>中提到的标签匹配tld。该tld文件应位于容器将查找的以下位置之一:

  1. 直接在WEB-INF里面
  2. 直接在 WEB-INF 的子目录中
  3. WEB-INF/lib/jar/META-INF/
  4. 在 WEB-INF/lib/jar/META-INF/ 的子目录中

希望这可以帮助

于 2013-06-08T17:19:47.437 回答