无法为 Java 中的自定义标记加载标记处理程序 class

Unable to load tag handler class for custom tag in Java

我在 Java 中使用自定义标签时出现错误。让我在下面详细说明涉及的文件:

我的tag.tld(路径是:\WEB-INF\tlds\tag.tld)有以下内容:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" 
                        "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">


<taglib>
<tlibversion>0.0</tlibversion>
<jspversion>1.1</jspversion>
<shortname>message</shortname>

<tag>
<description>StringReverseTag</description>
<name>string</name>
<tag-class>mytag.StringReverseTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>data</name>
<required>true</required>
</attribute>
</tag>
</taglib>

ReverseEx.jsp

<%@page contentType="text/html" pageEncoding="UTF-8" language="java"%>
<!DOCTYPE html>
<%@taglib uri="/WEB-INF/tlds/tag.tld" prefix="jen" %> 
<jen:string data="EARTH"/>

<html>
<head>
<title>Tag Example</title>
</head>
<body>
</body>
</html>

帮手class:

package chap4;

import java.io.IOException;
import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*;

public class StringReverseTag extends SimpleTagSupport{
    private String data;
    public void setData(String data) {
    this.data=data;
    }

    @Override
    public void doTag() throws JspException, IOException{
        JspWriter out = getJspContext().getOut();
        StringBuffer sb= new StringBuffer(data);
        sb.reverse();
        out.print(sb);
    }
}

我得到的错误如下:

org.apache.jasper.JasperException: file: ...Struts/Struts/build/web/ReverseEx.jsp(10,0) PWC6032: Unable to load tag handler class "mytag.StringReverseTag" for tag "jen:string" org.apache.jasper.JasperException: PWC6032: Unable to load tag handler class "mytag.StringReverseTag" for tag "jen:string" ...Struts/Struts/build/web/ReverseEx.jsp(10,0) ...Struts\Struts\nbproject\build-impl.xml:924: Java returned: 1 BUILD FAILED (total time: 2 seconds)

我做错了什么?

在你的 tag.tld 文件中你写:

<tag-class>mytag.StringReverseTag</tag-class>

这意味着:您的 class StringReverseTag 应该在 myTag 包中。 但是在你的 StringReverseTag 代码中我可以看到你有 chap4 作为包!

要解决您的问题,只需更改:

<tag-class>mytag.StringReverseTag</tag-class>

<tag-class>chap4.StringReverseTag</tag-class>