1.编写标签对应的实现类
package com.jiang.test;
import java.io.IOException;
import java.io.StringWriter;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class TestTag extends SimpleTagSupport {
/**
* 标签中的属性
*/
private String message;
public void setMessage(String msg) {
this.message = msg;
}
StringWriter sw = new StringWriter();
public void doTag() throws JspException, IOException {
System.out.println(message);
sw.write("<h1>");
getJspBody().invoke(sw);
getJspContext().getOut().println(sw.toString());
}
}
2.创建tld文件,文件放在web-inf下
<?xml version="1.0" encoding="UTF-8"?>
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>2.0</jsp-version>
<short-name>Example TLD with Body</short-name>
<tag>
<name>test</name>
<tag-class>com.jiang.test.TestTag</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>message</name>
</attribute>
</tag>
</taglib>
3.jsp文件中使用标签
<body>
<ts:test message="jiang">
1111111111111
</ts:test>
</body>