1.创建一个web项目
2.在webroot/web-inf下面创建一个普通文件,文件名扩展名为 tld,例如 mytag.tld
3.在tld文件中编写代码,例子如下,
<?xml version="1.0" encoding="UTF-8" ?>
<taglib 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"
version="2.1">
<tlib-version>1.1</tlib-version>
<short-name>my</short-name>
<uri>http://mytag.s1.tag</uri><!-- 随便定义 -->
<tag>
<name>date</name>
<tag-class>s1.Tag</tag-class> <!-- tag标签的实现类 -->
<body-content>empty</body-content>
</tag>
<tag>
<name>readFile</name>
<tag-class>s1.TagReadFile</tag-class>
<body-content>empty</body-content>
<attribute>
<name>file</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
<type>java.lang.String</type>
</attribute>
</tag>
</taglib>
4.上面的代码解释如下:
4.1 固定格式的头部(别忘了taglib有结束标签)
<?xml version="1.0" encoding="UTF-8" ?>
<taglib 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"
version="2.1">
4.2 <tlib-version>1.1</tlib-version> // 自定义标签的版本号
<short-name>my</short-name> //推荐的缩写,影响不大
<uri>http://mytag.s1.tag</uri> //定位标签库的位置,在jsp页面中通过这个关联标签库
<tag> //定义一个标签
<name>readFile</name> //标签的名称
<tag-class>s1.TagReadFile</tag-class> 标签的实现类,标签本身没用,一定要有一个类来执行相应的功能
<body-content>empty</body-content> 一般是empty
<attribute>
<name>file</name> //属性名
<required>true</required> //属性是否必须设置
<rtexprvalue>true</rtexprvalue> //运行时的值
<type>java.lang.String</type> //属性的值属于什么类
</attribute>
5.创建一个class文件,专门用来实现标签的功能
6.实现SimpleTag 接口,并继承所有的方法
public class Tag implements SimpleTag{
7.在setJspContext()方法中将传入的pageContext对象缓存
public void setJspContext(JspContext jc) {
this.pageContext=(PageContext)jc;
//就是传入网页的 PageContext对象,拿到这个对象之后,就可以执行之后的操作
}
8. 在dotag()方法中执行所有的操作
public void doTag() throws JspException, IOException {
//这里就是标签要执行的代码
}
9.完整实例代码如下:
package s1;
import java.io.IOException;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.jsp.JspContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.JspTag;
import javax.servlet.jsp.tagext.SimpleTag;
public class Tag implements SimpleTag{
private PageContext pageContext=null;
public void setJspContext(JspContext jc) {
this.pageContext=(PageContext)jc;
//就是传入网页的 PageContext对象,拿到这个对象之后,就可以执行之后的操作
SimpleDateFormat sm=new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
String date=sm.format(new Date());
try {
pageContext.getOut().print(date);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args){
}
public void doTag() throws JspException, IOException {
//这里就是标签要执行的代码
System.out.println("doTag()");
}
public JspTag getParent() {
System.out.println("getParent()");
return null;
}
public void setJspBody(JspFragment arg0) {
System.out.println("setJspBody()");
}
public void setParent(JspTag arg0) {
System.out.println("setParent()");
}
}