I got the error “HtmlEncoderTag cannot be resolved

2019-09-05 16:23发布

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?

2条回答
我命由我不由天
2楼-- · 2019-09-05 16:45

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>
查看更多
趁早两清
3楼-- · 2019-09-05 16:53

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.
查看更多
登录 后发表回答