Example of JSP taglib and JSPtaglib
Web. xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 5 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 6 7 <jsp-config> 8 <taglib> 9 <taglib-uri>/WEB-INF</taglib-uri>10 <taglib-location>/WEB-INF/mytag.tld</taglib-location>11 </taglib>12 </jsp-config>13 </web-app>
Mytag. tld
1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <taglib> 3 <tlibversion> 1.0 </tlibversion> 4 <jspversion> 1.1 </jspversion> 5 6 <tag> 7 <name> displayDate </name> 8 <tagclass> gys. dateTagNoBody </tagclass> 9 <bodycontent> empty </bodycontent> <! -- The content between tag bodies should be blank --> 10 </tag> 11 <tag> 12 <name> displayDateTwo </name> 13 <tagclass> gys. dateTagAttribute </tagclass> 14 <bodycontent> empty </bodycontent> <! -- The content between tags must be blank --> 15 <! -- Define Attributes --> 16 <attribute> 17 <name> pattern </name> <! -- Attribute name --> 18 <type> String </type> <! -- Attribute type --> 19 <requried> false </requried> <! -- Required? --> 20 <rtexprvale> false </rtexprvale> <! -- Indicates whether JSP expressions can be used --> 21 </attribute> 22 </tag> 23 24 <tag> 25 <name> iterator </name> 26 <tagclass> gys. bodyTag </tagclass> 27 <bodycontent> jsp </bodycontent> 28 <! -- Define Attributes --> 29 <attribute> 30 <name> count </name> <! -- Attribute name --> 31 <type> int </type> <! -- Property type --> 32 <requried> false </requried> <! -- Required? --> 33 <rtexprvale> false </rtexprvale> <! -- Indicates whether JSP expressions can be used --> 34 </attribute> 35 </tag> 36 37 </taglib>
BodyTag. java
1 package gys; 2 3 import java. io. IOException; 4 import java. text. simpleDateFormat; 5 import java. util. date; 6 7 import javax. servlet. http. httpServletRequest; 8 import javax. servlet. jsp. jspException; 9 import javax. servlet. jsp. jspWriter; 10 import javax. servlet. jsp. tagext. bodyContent; 11 import javax. servlet. jsp. tagext. bodyTagSupport; 12 13 public class BodyTag extends BodyTagSupport {14 15 // execution sequence 16 // 17 // doStartTag ()-> setBodyContent ()-> doInitBody ()-> doAfterTag ()-> doEndTag () 18 // 19 // If doStartTag () the returned value is EVAL_BODY_INCLUDE, which executes the doAfterTag () method. 20 // 21 // if it returns SKIP_BODY, The doEndTag () method is executed. 22 // 23 // setBodyContent () method is used to set the content of the label body. If Initialization is required during BodyContent calculation, 24 // 25 () method. After the label body content is executed, the doAfterBody () method 26 // 27 // returns EVAL_BODY_AGAIN in the doAfterTag () method to repeatedly execute doAfterTag () method 28 // 29 // If the SKIP_BODY value is returned, the doEndTag () method is executed. 30 // 31 // If the EVAL_PAGE value is returned in the doEndTag () method, other code after the label is executed, 32 // 33 // If SKIP_PAGE is returned, other code on this page is not executed. 34 35 private int count; 36 37 private HttpServletRequest reqeust; 38 39 private JspWriter out; 40 41 public void init () {42 reqeust = (HttpServletRequest) pageContext. getRequest (); 43 out = pageContext. getOut (); 44} 45 46 @ Override47 public int doStartTag () throws JspException {48 init (); 49 return this. EVAL_BODY_INCLUDE; // execute doAterTag () method 50} 51 52 // set the current TAG body 53 @ Override54 public void setBodyContent (BodyContent bodyContent) {55 this. bodyContent = bodyContent; 56 // System. out. println ("setBodyContent... "); 57} 58 59 // You Need to initialize bodyContent60 @ Override61 public void doInitBody () throws JspException {62 System. out. println ("init ..... "); 63} 64 65 @ Override66 public int doAfterBody () throws JspException {67 if (count> = 1) {68 try {69 out. println (count); 70 out. println ("<Br>"); 71} catch (IOException e) {72 e. printStackTrace (); 73} 74 count --; 75 return this. EVAL_BODY_AGAIN; // return in doAfterBody, repeated doAfterBody method 76} else {77 return this. SKIP_BODY; // execute doEndTag78} 79} 80 81 @ Override82 public int doEndTag () throws JspException {83 SimpleDateFormat formater = new SimpleDateFormat ("yyyy-MM-dd "); 84 String date = formater. format (new Date (); 85 try {86 out. print (date); 87} catch (IOException e) {88 e. printStackTrace (); 89} 90 return this. EVAL_PAGE; // do not execute other code on this page 91} 92 93 // The setXX () method 94 public void setCount (int count) {95 System must be implemented. out. println ("count ........ "); 96 this. count = count; 97} 98}
DateTagAttributr. java
1 package gys; 2 3 import java. io. IOException; 4 import java. text. simpleDateFormat; 5 import java. util. date; 6 7 import javax. servlet. http. httpServletRequest; 8 import javax. servlet. jsp. jspException; 9 import javax. servlet. jsp. jspWriter; 10 import javax. servlet. jsp. tagext. tag; 11 import javax. servlet. jsp. tagext. tagSupport; 12 13 public class DateTagAttribute extends TagSupport {14 15 private String pa Ttern; 16 17 @ Override18 public int doStartTag () throws JspException {19 HttpServletRequest request; 20 // an attribute defined in the TagSupport class, which is javax. servlet. jsp. object 21 request = (HttpServletRequest) PageContext of pageContext. getRequest (); 22 SimpleDateFormat formater = new SimpleDateFormat (pattern); 23 String date = formater. format (new Date (); 24 JspWriter out = pageContext. getOut (); 25 try {26 out. print (date); 27} cat Ch (IOException e) {28 e. printStackTrace (); 29} 30 // The doStartTag () method returns the SKIP_BODY. Of course, the reason is that our simple date tag does not have a body. 31 return Tag. SKIP_BODY; 32} 33 34 // The setXX () method must be implemented 35 public void setPattern (String pattern) {36 this. pattern = pattern; 37} 38}
DateTagNoBody. java
1 package gys; 2 3 import java.io.IOException; 4 import java.text.SimpleDateFormat; 5 import java.util.Date; 6 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.jsp.JspException; 9 import javax.servlet.jsp.JspWriter;10 import javax.servlet.jsp.tagext.Tag;11 import javax.servlet.jsp.tagext.TagSupport;12 13 public class DateTagNoBody extends TagSupport {14 15 @Override16 public int doStartTag() throws JspException {17 HttpServletRequest request=(HttpServletRequest)pageContext.getRequest();18 SimpleDateFormat formater=new SimpleDateFormat("yyyy-MM-dd");19 String date=formater.format(new Date());20 JspWriter out=pageContext.getOut();21 try{22 out.print(date);23 }24 catch(IOException e){25 e.printStackTrace();26 }27 return Tag.SKIP_BODY;28 }29 30 }
Running result: