我正在尝试在JSP上自定义标签。我遵循了一个教程,并最终获得了以下代码:
Taglib导入:
<%@taglib prefix="me" uri="/WEB-INF/tlds/myTLD.tld" %>
在这里,我实现了我的标签:
<body>
<h1>Testing custom tags</h1>
<me:MiTag titulo="Some title">
A test text
</me:MiTag>
</body>
这是我的TLD外观(由NetBeans生成):
<?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>mytld</short-name>
<uri>/WEB-INF/tlds/myTLD</uri>
<tag>
<name>MiTag</name>
<tag-class>MiTag</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>titulo</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
<type>java.lang.String</type>
</attribute>
</tag>
</taglib>
这是我的标记处理程序类:
public class MiTag extends SimpleTagSupport {
private String titulo;
@Override
public void doTag() throws JspException {
JspWriter out = getJspContext().getOut();
try {
out.println("<h3>"+titulo+"</h3>");
out.println(" <blockquote>");
JspFragment f = getJspBody();
if (f != null) {
f.invoke(out);
}
out.println(" </blockquote>");
} catch (java.io.IOException ex) {
throw new JspException("Error in MiTag tag", ex);
}
}
/**
* @param titulo the Titulo to set
*/
public void setTitulo(String titulo) {
this.titulo = titulo;
}
}
好吧,这应该工作。但...:
怎么了
最佳答案
我意识到出了什么问题:标记处理程序类不在包中。一旦我将类放入包中(例如,“标签”),并通过
<tag-class>tags.MiTag</tag-class>
...开始工作了!