无法加载标记处理程序 class
Unable to load tag handler class
我在标签包中有这个 class 用于 tld 文件
package tag;
import java.io.IOException;
import java.time.LocalDate;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
public class printDateTag extends TagSupport{
@Override
public int doStartTag() throws JspException {
try {
JspWriter writer = pageContext.getOut();
writer.print(LocalDate.now());
} catch (IOException ex) {System.out.println(ex);}
return SKIP_BODY;
}
}
我使用那个 class 的 tld 文件是这个
<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.1" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd">
<tlib-version>1.0</tlib-version>
<short-name>tags</short-name>
<uri>/WEB-INF/tlds/tags</uri>
<tag>
<name>printDate</name>
<tag-class>tag.printDateTag</tag-class>
</tag>
我出错的 jsp 是这个
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="ta" uri="/WEB-INF/tlds/tags.tld"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<ta:printDate/>
<a href="index">go</a>
</body>
</html>
这里 <ta:printDate/>
我有错误 无法加载标签处理程序 class "tag.printDateTag" 标签“ta:printDate
根据您的代码,
looks like your tld file is not properly configure,
见下面我试过的代码,
printDateTag.java
package tag;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
public class printDateTag extends TagSupport{
@Override
public int doStartTag() throws JspException {
try {
JspWriter writer = pageContext.getOut();
writer.print("<u>Hello From Tag</u>"); // <u>Hello From Tag</u>... it will display on JSP page
} catch (IOException ex) {System.out.println(ex);}
return SKIP_BODY;
}
}
tags.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/j2ee/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>simple</short-name>
<!-- <uri>http://tomcat.apache.org/example-taglib</uri> -->
<uri>/WEB-INF/tlds/tags</uri>
<tag>
<name>printDate</name>
<tag-class>tag.printDateTag</tag-class>
</tag>
</taglib>
test.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="ta" uri="/WEB-INF/tlds/tags.tld"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<ta:printDate/>
<a href="index">go</a>
</body>
</html>
输出:
我在标签包中有这个 class 用于 tld 文件
package tag;
import java.io.IOException;
import java.time.LocalDate;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
public class printDateTag extends TagSupport{
@Override
public int doStartTag() throws JspException {
try {
JspWriter writer = pageContext.getOut();
writer.print(LocalDate.now());
} catch (IOException ex) {System.out.println(ex);}
return SKIP_BODY;
}
}
我使用那个 class 的 tld 文件是这个
<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.1" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd">
<tlib-version>1.0</tlib-version>
<short-name>tags</short-name>
<uri>/WEB-INF/tlds/tags</uri>
<tag>
<name>printDate</name>
<tag-class>tag.printDateTag</tag-class>
</tag>
我出错的 jsp 是这个
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="ta" uri="/WEB-INF/tlds/tags.tld"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<ta:printDate/>
<a href="index">go</a>
</body>
</html>
这里 <ta:printDate/>
我有错误 无法加载标签处理程序 class "tag.printDateTag" 标签“ta:printDate
根据您的代码,
looks like your tld file is not properly configure,
见下面我试过的代码,
printDateTag.java
package tag;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
public class printDateTag extends TagSupport{
@Override
public int doStartTag() throws JspException {
try {
JspWriter writer = pageContext.getOut();
writer.print("<u>Hello From Tag</u>"); // <u>Hello From Tag</u>... it will display on JSP page
} catch (IOException ex) {System.out.println(ex);}
return SKIP_BODY;
}
}
tags.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/j2ee/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>simple</short-name>
<!-- <uri>http://tomcat.apache.org/example-taglib</uri> -->
<uri>/WEB-INF/tlds/tags</uri>
<tag>
<name>printDate</name>
<tag-class>tag.printDateTag</tag-class>
</tag>
</taglib>
test.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="ta" uri="/WEB-INF/tlds/tags.tld"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<ta:printDate/>
<a href="index">go</a>
</body>
</html>
输出: