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

旧时模样 提交于 2020-01-06 05:23:11

问题


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.IOException;
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?


回答1:


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.



回答2:


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>


来源:https://stackoverflow.com/questions/6922785/i-got-the-error-htmlencodertag-cannot-be-resolved-to-a-type-on-defining-a-cust

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!