我在tag pacage中有这个类,它用于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;
}
}
我使用该类的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/>
我有错误无法为标签“ta:printDate
答案 0 :(得分:0)
AS见过你的代码,
看起来你的tld文件没有正确配置,
请参阅以下我尝试过的代码,
<强> 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>
输出: