无法在Java

时间:2015-09-02 11:43:56

标签: java jsp servlets custom-tags

我正在使用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>

助手类:

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:无法加载   标记处理程序类&#34; mytag.StringReverseTag&#34;标签&#34; jen:string&#34;   org.apache.jasper.JasperException:PWC6032:无法加载标记   处理程序类&#34; mytag.StringReverseTag&#34;标签&#34; jen:string&#34;   ...的Struts / Struts的/编译/网络/ ReverseEx.jsp(10,0)   ... Struts \ Struts \ nbproject \ build-impl.xml:924:Java返回:1 BUILD   失败(总时间:2秒)

我做错了什么?

1 个答案:

答案 0 :(得分:5)

在您编写的 tag.tld 文件中:

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

这意味着:您的班级StringReverseTag应该在myTag个套餐中。 但是在StringReverseTag的代码中,我可以看到你有chap4作为包裹!

解决你的问题只需改变:

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

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