如何为JSP定义自定义标记?

时间:2017-11-07 23:44:08

标签: java jsp servlets

我按照书籍servlet -jsp教程编写以下代码: SRC / MyFirstTag.java

public class MyFirstTag implements SimpleTag {
    JspContext jspContext;

    @Override
    public void doTag() throws JspException, IOException {
        System.out.println("doTag");
        jspContext.getOut().print("This is my first tag.");
    }


    @Override
    public void setJspContext(JspContext jspContext) {
        System.out.println("setJspContext");
        this.jspContext = jspContext;
    }

}

并注册标记/web/WEB-INF/mytags.tld

<?xml version="1.0" encoding="UTF-8"?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_1.xsd"
        version="2.0">

    <description>
        Simple tag examples
    </description>
    <tlib-version>1.0</tlib-version>
    <short-name>example</short-name>
    <tag>
        <name>firstTag</name>
        <tag-class>MyFirstTag</tag-class>
        <body-content>empty</body-content>
    </tag>
</taglib>

最后是JSP文件:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="/WEB-INF/mytags.tld" prefix="easy"%>
<html>
<head>
    <title>Title</title>
</head>
<body>
<easy:firstTag></easy:firstTag>
</body>
</html>

但它一直告诉我

HTTP Status 500 – Internal Server Error
Type Exception Report

Message java.lang.ClassNotFoundException: org.apache.jsp.tagtest_jsp

org.apache.jasper.JasperException: java.lang.ClassNotFoundException: org.apache.jsp.tagtest_jsp

为什么无法找到自定义标签?我认为taglib uri="/WEB-INF/mytags.tld"<tag-class>MyFirstTag</tag-class>应该给出java类的路径。

0 个答案:

没有答案