dui对应的tld文件,且放在WEB-INF目录下
<?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"> <description>WQ的标签库</description> <display-name>WQ</display-name> <tlib-version>1.1</tlib-version> <short-name>c3</short-name> <uri>/jsp1/tag</uri> <tag> <description>根据指定的次数打印文本</description> <name>pt</name> <tag-class>tag.HelloTag</tag-class> <body-content>empty</body-content> <attribute> <description>Content</description> <name>str</name> <required>false</required> <rtexprvalue>false</rtexprvalue> </attribute> <attribute> <description>Count</description> <name>count</name> <required>false</required> <rtexprvalue>false</rtexprvalue> </attribute> </tag> </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"> <description>这是我自己的标签库</description> <display-name>My Tag</display-name> <tlib-version>3.1</tlib-version> <short-name>s</short-name> <uri>/WQ-tags</uri> <tag> <description>用来输出当前服务器的时间,并且时间的格式可以任意指定</description> <name>sysdate</name> <tag-class>tag.SysdateTag</tag-class> <!-- 声明该标签可以包含哪些内容 该例为但标签,没有内容 --> <body-content>empty</body-content> <attribute> <description>用来设置时间的格式</description> <name>format</name> <!-- 是否必须给这个属性赋值 --> <required>false</required> <!-- 是否可以用EL给此属性赋值 --> <rtexprvalue>true</rtexprvalue> <type>java.lang.String</type> </attribute> </tag> </taglib>jsp测试文件:
<%@page pageEncoding="utf-8" contentType="text/html; charset=utf-8" %> <%@taglib uri="/jsp1/tag" prefix="c3" %> <%@taglib uri="/WQ-tags" prefix="s"%> <!doctype html> <html> <head> <meta charset="utf-8" /> <title>自定义标签</title> </head> <body> <c3:pt count="10" str="Hello JSTL"/> <br><hr> <s:sysdate /> <br><hr> <s:sysdate format="yyyy'年'MM'月'dd'日' HH'时'mm'分'ss'秒'"/> <br><hr> </body> </html>输出结果:Hello JSTL Hello JSTL Hello JSTL Hello JSTL Hello JSTL Hello JSTL Hello JSTL Hello JSTL Hello JSTL Hello JSTL
注意:内容可以参考标准标签库中的c.tld文件的内容,在使用JSTL标签前必须要写相应的指令-----taglib
标签的运行原理:
容器依据JSP页面中的uri找到tld文件(依据标签中的<c3:pt>pt这个名字找到标签类tag.HelloTag。接下来实例化该标签,同时属性值赋给参数,调用doTag方法