开发者

I got the error "HtmlEncoderTag cannot be resolved to a type" on defining a custom tag?

开发者 https://www.devze.com 2023-03-24 15:43 出处:网络
I am defining a custom tag \"htmlencoder\". I have These files: WEB-INF/classes/HtmlEncoderTag.jar ,with a java code like this:

I am defining a custom tag "htmlencoder". I have These files:

WEB-INF/classes/HtmlEncoderTag.jar ,with a java code like this:

import java.io.I开发者_JAVA百科OException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class HtmlEncoderTag extends BodyTagSupport{
    //....
}

WEB-INF/htmlencoder.tld :

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib
        PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
        "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
  <tlib-version>1.0</tlib-version>
  <jsp-version>1.2</jsp-version>
  <short-name></short-name>
 <tag>
    <name>htmlencode</name>
    <tag-class>HtmlEncoderTag</tag-class>
    <body-content>JSP</body-content>
  </tag>
</taglib>

index.jsp:

<%@ taglib uri="WEB-INF/htmlencoder.tld" prefix="htmlencoder"%>
<head>
  <title>Watch out you sinners...</title>
</head>
<html>
  <body bgcolor="white">
    <htmlencoder:htmlencode><script <% //the error refers to this line %>
      type="javascript">BadStuff()</script></htmlencoder:htmlencode>
  </body>
</html>

I got the error "HtmlEncoderTag cannot be resolved to a type" when i run my page and it shows me the line index.jsp:7 ( I mentioned above).

What should I do?


I found the answer. I'm not sure it is necessary, but my problem solved this way: You have to put your tag class in a package. for example, your HtmlEncoderTag.jar file should be placed in WEB-INF/MyTag/ and should be like this:

pckage MyTag;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class HtmlEncoderTag extends BodyTagSupport{
    //....
}

And your tld file will be like this:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib
        PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
        "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
  <tlib-version>1.0</tlib-version>
  <jsp-version>1.2</jsp-version>
  <short-name></short-name>
 <tag>
    <name>htmlencode</name>
    <tag-class>MyTag.HtmlEncoderTag</tag-class>
    <body-content>JSP</body-content>
  </tag>
</taglib>


Two things:

  1. HtmlEncoderTag.jar should be in WEB-INF/lib, not WEB-INF/classes.
  2. Is HtmlEncoderTag in a package? None is shown in your Java code. However, if it is, the class name in the tag-class element needs to be fully qualified.
0

精彩评论

暂无评论...
验证码 换一张
取 消