step1. 导包(导入要使用的标签的jar文件)。step2. 使用taglib指令引入要使用的标签。taglib指令: uri:标签的命名空间。 prefix:命名空间的别名。注: 命名空间:是为了区分同名的元素而添加的前缀。
自定义标签:
step1. 写一个java类,继承SimpleTagSupport类。step2. 在doTag方法里面,编写处理逻辑。step3. 描述标签。(.tld文件里面) 注: <body-content>的值可以是 empty 没有标签体。 scriptless 可以有标签体,但是标签体的内容不能够出现 java代码。 JSP 可以有标签体,标签体的内容可以出现java代码。 (只有复杂标签技术才支持JSP这个值,简单标签技术只支持 empty和scriptless)。
简单日期标签datetag.tld例:
<?XML version= "1.0" encoding= "UTF-8"?><taglibxmlns= "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>D1</Short-name> <URI>Http://aliyun.com</URI> <Tag> <!--Name: Indicates the custom label <c1:date the same name - <name>Date</name> <Tag-class>Tag. DateTag</Tag-class> <body-content>Empty</body-content> <attribute> <!--Name: Indicates the custom label <c1:date DateTag name is the same - <name>DateTag</name> <!--true to indicate that an EL expression can be used to assign a value - <Required>True</Required> <Rtexprvalue>False</Rtexprvalue> </attribute> </Tag></taglib>
日期java标签类:
Packagetag;/*** Custom Date labels*/Importjava.io.IOException;ImportJava.text.SimpleDateFormat;Importjava.util.Date;Importjavax.servlet.jsp.JspException;ImportJavax.servlet.jsp.JspWriter;ImportJavax.servlet.jsp.PageContext;ImportJavax.servlet.jsp.tagext.SimpleTagSupport; Public classDateTagextendsSimpletagsupport {PrivateString DateTag; PublicString Getdatetag () {returnDateTag; } Public voidSetdatetag (String datetag) { This. DateTag =DateTag; } @Override Public voidDotag ()throwsjspexception, IOException {pagecontext pc=(PageContext) getjspcontext (); SimpleDateFormat SDF=NewSimpleDateFormat (DateTag); String Date= Sdf.format (NewDate ()); JspWriter out=pc.getout (); OUT.PRINTLN (date); }}
Jstl Tag Basics Development Steps