首页 > 代码库 > 传统标签库功能

传统标签库功能

 

 1 package cn.itcast.web.tag; 2  3 import javax.servlet.jsp.JspException; 4 import javax.servlet.jsp.tagext.IterationTag; 5 import javax.servlet.jsp.tagext.Tag; 6 import javax.servlet.jsp.tagext.TagSupport; 7  8 //控制表前提执行5次 9 public class TagDemo3 extends TagSupport {10     int x=5;11     @Override12     public int doAfterBody() throws JspException {13             14             x--;15             if(x>0){16                 return IterationTag.EVAL_BODY_AGAIN;17             }else{18                 return IterationTag.SKIP_BODY;19             }20     }21 22     @Override23     public int doStartTag() throws JspException {24 25         return Tag.EVAL_BODY_INCLUDE;26     }27     28     29 30 }
TagDemo3
 1 package cn.itcast.web.tag; 2  3 import java.io.IOException; 4  5 import javax.servlet.jsp.JspException; 6 import javax.servlet.jsp.tagext.BodyContent; 7 import javax.servlet.jsp.tagext.BodyTag; 8 import javax.servlet.jsp.tagext.BodyTagSupport; 9 import javax.servlet.jsp.tagext.Tag;10 11 //修改标签体(把标签体改为大写)12 public class TagDemo4 extends BodyTagSupport {13 14     @Override15     public int doEndTag() throws JspException {16 17         BodyContent bc = this.getBodyContent();//得到标签体18         String content = bc.getString();19         content = content.toUpperCase();20         21         try {22             this.pageContext.getOut().write(content);23         } catch (IOException e) {24             throw new RuntimeException(e);25         }26         27         return Tag.EVAL_PAGE;28         29     }30 31     @Override32     public int doStartTag() throws JspException {33 34         return BodyTag.EVAL_BODY_BUFFERED;35     }36     37     38 39 }
TagDemo4
 1 <?xml version="1.0" encoding="UTF-8" ?> 2  3  4 <taglib xmlns="http://java.sun.com/xml/ns/j2ee" 5     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 6     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" 7     version="2.0"> 8     <description>A tag library exercising SimpleTag handlers.</description> 9     <tlib-version>1.0</tlib-version>10     <short-name>itcast</short-name>11     <uri>/itcast</uri>12     13     <tag>14         <name>viewIP</name>15         <tag-class>cn.itcast.web.tag.ViewIPTag</tag-class>16         <body-content>empty</body-content>17     </tag>18     <tag>19         <name>TagDemo3</name>20         <tag-class>cn.itcast.web.tag.TagDemo3</tag-class>21         <body-content>JSP</body-content>22     </tag>23     <tag>24         <name>TagDemo4</name>25         <tag-class>cn.itcast.web.tag.TagDemo4</tag-class>26         <body-content>JSP</body-content>27     </tag>28    29   30   </taglib>
itcast.tld