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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32352250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 20:04:26  来源:igfitidea点击:

Unable to load tag handler class for custom tag in Java

javajspservletscustom-tags

提问by Janine

I am working with custom tags in Java and I am getting an error. Let me detail below the files involved:

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

My tag.tld (path is: \WEB-INF\tlds\tag.tld) has the following content:

我的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

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>

Helper 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);
    }
}

The error I get is the following:

我得到的错误如下:

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)

org.apache.jasper.JasperException:文件:...Struts/Struts/build/web/ReverseEx.jsp(10,0) PWC6032:无法为标签“jen:string”加载标签处理程序类“mytag.StringReverseTag” .apache.jasper.JasperException:PWC6032:无法为标签“jen:string”加载标签处理程序类“mytag.StringReverseTag”...Struts/Struts/build/web/ReverseEx.jsp(10,0) ...Struts \Struts\nbproject\build-impl.xml:924:Java 返回:1 BUILD FAILED(总时间:2 秒)

What am I doing wrong?

我究竟做错了什么?

回答by Rami.Q

in your tag.tldfile you write:

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

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

this means: your class StringReverseTagshould be in myTagpackage. but in your code of StringReverseTagi can see that you have chap4as package!

这意味着:您的课程StringReverseTag应该在myTag包中。但在你的代码中StringReverseTag我可以看到你有一个chap4包!

to solve your problem just change:

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

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

to

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