1 。 创建一个类实现 SimpleTag 接口
public class customTagLib implements SimpleTag {
@Override
public void doTag() throws JspException, IOException {
// TODO Auto-generated method stub
}
@Override
public void setParent(JspTag parent) {
// TODO Auto-generated method stub
}
@Override
public JspTag getParent() {
// TODO Auto-generated method stub
return null;
}
@Override
public void setJspContext(JspContext pc) {
// TODO Auto-generated method stub
}
@Override
public void setJspBody(JspFragment jspBody) {
// TODO Auto-generated method stub
}
}
- 在dotag方法中编写自己想要实现的内容
@Override
public void doTag() throws JspException, IOException {
// TODO Auto-generated method stub
JspWriter out = context.getOut();
out.print("这个是我的第一个自定义的标签");
3。 创建tld文件
<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 http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<description>A tag library exercising SimpleTag handlers.</description>
<tlib-version>1.0</tlib-version>
<short-name>pkuie</short-name>
<uri>http://www.baidu.com/pkuie</uri>
<tag>
<name>hello</name>
<tag-class>jsp.customTagLib</tag-class>
<body-content>empty</body-content>
</tag>
</taglib>
- 使用
<%@ taglib prefix="pkuie" uri="http://www.baidu.com/pkuie"%>
<pkuie:hello/>
网友评论