zoukankan      html  css  js  c++  java
  • 自定义标签

    一、创建自定义标签基本步骤

    1、步骤

    标签处理类(标签也是一个对象,那么就需要先有类!)

    tld文件,它是一个xml

    页面中使用<%@taglib%>来指定tld文件的位置

    2、标签处理类

    SimpleTag接口

    • void doTag():每次执行标签时都会调用这个方法;
    • JspTag getParent():返回父标签(非生命周期方法)
    • void setParent(JspTag):设置父标签
    • void setJspBody(JspFragment):设置标签体
    • void setJspContext(JspContext):设置jsp上下文对象,它的子类是PageContext

      其中doTag()会在其他三个方法之后被Tomcat调用。

    3、配置tld文件

    tld文件一般都放到WEB-INF之下,这样保证客户端访问不到。

    <tag>
           <name>MyTag1</name>   指定当前标签的名称
           <tag-class>tag.MyTag1</tag-class>  指定当前标签的标签处理类
           <body-content>empty</body-content> 指定标签体的类型,我们这里使用的是空标签。
    </tag>

    4、页面中指定tld文件位置

     二、标签体内容

    1、标签体内容

    • empty:无标签体。
    • JSP:jsp2.0已经不再支持这个类型。表示标签体内容可以是:Java脚本,可以是标签,可以是EL表达式。
    • scriptless:只能是EL表达式,也可以是其他标签。
    • tagdependent:标签体内容不会被执行,而是直接赋值标签处理类。

    2、不再执行标签下面内容的标签。

    • 在标签处理类中的doTag()使用SkipPageException来结束。
    • Tomcat会调用标签处理类的doTag()方法,然后Tomcat会得到SkipPageException,它会跳过本页面其他内容。

    三、标签属性

    步骤:

    1、给你的标签处理类添加属性。

    为标签处理类添加属性,属性至少要且一个set方法,这个set方法会在doTage()方法之前被Tomcat执行,所在doTage()中就可以使用属性了。

    2、在tld文件中对属性进行配置。

    <attribute>
        <name>test</name>  指定属性名
        <required>true</required>  指定属性是否为必需的
        <rtexprvalue>true</rtexprvalue> 指定属性是否可以使用EL表达式
    </attribute>

    四、示例:

     1 package tag;
     2 
     3 import javax.servlet.jsp.JspContext;
     4 import javax.servlet.jsp.JspException;
     5 import javax.servlet.jsp.PageContext;
     6 import javax.servlet.jsp.tagext.JspFragment;
     7 import javax.servlet.jsp.tagext.JspTag;
     8 import javax.servlet.jsp.tagext.SimpleTag;
     9 import java.io.IOException;
    10 
    11 /**
    12  * 自定义标签
    13  */
    14 public class MyTag1 implements SimpleTag {
    15     private PageContext pageContext;
    16     private JspFragment body;
    17     /**
    18      * 所有的setXXX()方法都会在doTag()方法之前被Tomcat调用。
    19      * 所有doTag()中就可以使用Tomcat传递过来的对象了。
    20      * */
    21     @Override
    22     public void doTag() throws JspException, IOException {
    23         pageContext.getOut().print("Hello Tag!");
    24     }
    25     @Override
    26     public void setParent(JspTag jspTag) {}
    27     @Override
    28     public JspTag getParent() {return null; }
    29     @Override
    30     public void setJspContext(JspContext context) {
    31         this.pageContext = (PageContext) context;
    32     }
    33     @Override
    34     public void setJspBody(JspFragment body) {
    35         this.body = body;
    36     }
    37 }
     1 package tag;
     2 
     3 import javax.servlet.jsp.JspException;
     4 import javax.servlet.jsp.tagext.SimpleTagSupport;
     5 import java.io.IOException;
     6 
     7 /**
     8  * SimpleTagSupport它实现了SimpleTag接口。
     9  * 它已经把所有的Tomcat传递的数据都保存起来了,而且还提供了get方法子类调用。
    10  */
    11 public class MyTag2 extends SimpleTagSupport {
    12     @Override
    13     public void doTag() throws JspException, IOException {
    14         this.getJspContext().getOut().print("Hello 第二次");
    15     }
    16 }
     1 package tag;
     2 
     3 import javax.servlet.jsp.JspException;
     4 import javax.servlet.jsp.tagext.SimpleTagSupport;
     5 import java.io.IOException;
     6 import java.io.Writer;
     7 
     8 public class MyTag3 extends SimpleTagSupport {
     9     @Override
    10     public void doTag() throws JspException, IOException {
    11         Writer out = this.getJspContext().getOut();//获取当前jsp页面的输出流
    12         out.write("****************************<br/>");
    13         this.getJspBody().invoke(out);//执行标签体内容,把结果写到指定的流中,即页面上
    14         out.write("<br/>****************************");
    15     }
    16 }
     1 package tag;
     2 
     3 import javax.servlet.jsp.JspException;
     4 import javax.servlet.jsp.SkipPageException;
     5 import javax.servlet.jsp.tagext.SimpleTagSupport;
     6 import java.io.IOException;
     7 
     8 public class MyTag4 extends SimpleTagSupport {
     9     @Override
    10     public void doTag() throws JspException, IOException {
    11         this.getJspContext().getOut().print("只能看到我,下面什么都没有!");
    12         throw new SkipPageException();//抛出这个异常后,在本标签后面的内容,将看不到。
    13     }
    14 }
     1 package tag;
     2 
     3 import javax.servlet.jsp.JspException;
     4 import javax.servlet.jsp.tagext.SimpleTagSupport;
     5 import java.io.IOException;
     6 
     7 /**
     8  * 有属性的标签
     9  */
    10 public class MyTag5 extends SimpleTagSupport {
    11     private boolean test;
    12 
    13     /**
    14      * 这个方法会有Tomcat来调用,并在doTage()之前
    15      * */
    16     public void setTest(boolean test) {
    17         this.test = test;
    18     }
    19     @Override
    20     public void doTag() throws JspException, IOException {
    21         /**
    22          * 执行标签体
    23          * */
    24         if (test) {
    25             this.getJspBody().invoke(null);//如果传递的输出流为null,表示使用的就是当前页面的out。
    26         }
    27     }
    28 }
     1 <?xml version="1.0" encoding="UTF-8" ?>
     2 
     3 <taglib xmlns="http://java.sun.com/xml/ns/j2ee"
     4         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     5         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
     6         version="2.0">
     7 
     8     <tlib-version>1.0</tlib-version>
     9     <short-name>itcast</short-name>
    10     <uri>http://www.itcast.cn/tags/it-1.0</uri>
    11     <tag>
    12         <name>MyTag1</name>
    13         <tag-class>tag.MyTag1</tag-class>
    14         <body-content>empty</body-content>
    15     </tag>
    16     <tag>
    17         <name>MyTag2</name>
    18         <tag-class>tag.MyTag2</tag-class>
    19         <body-content>empty</body-content>
    20     </tag>
    21     <tag>
    22         <name>MyTag3</name>
    23         <tag-class>tag.MyTag3</tag-class>
    24         <body-content>scriptless</body-content>
    25     </tag>
    26     <tag>
    27         <name>MyTag4</name>
    28         <tag-class>tag.MyTag4</tag-class>
    29         <body-content>empty</body-content>
    30     </tag>
    31     <tag>
    32         <name>MyTag5</name>
    33         <tag-class>tag.MyTag5</tag-class>
    34         <body-content>scriptless</body-content>
    35         <attribute>
    36             <name>test</name>
    37             <required>true</required>
    38             <rtexprvalue>true</rtexprvalue>
    39         </attribute>
    40     </tag>
    41 </taglib>
     1 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
     2 <%@ taglib prefix="it" uri="/WEB-INF/tlds/itcast.tld" %>
     3 <html>
     4   <head>
     5     <title>$Title$</title>
     6   </head>
     7   <body>
     8 
     9 <it:MyTag5 test="${empty param.xxx}">
    10   <h1><it:MyTag4/></h1>
    11 </it:MyTag5>
    12   <h1><it:MyTag1/></h1>
    13   <h1><it:MyTag2/></h1>
    14   <hr/>
    15     <%
    16     request.setAttribute("xxx","zhangsan");
    17     %>
    18   <h3><it:MyTag3>${xxx}</it:MyTag3></h3>
    19   <h3><it:MyTag3>字符串®</it:MyTag3></h3>
    20 
    21   </body>
    22 </html>
  • 相关阅读:
    南阳oj 82 迷宫寻宝(一)
    杭电 oj 1016 Prime Ring Problem
    杭电 oj 3350 #define is unsafe
    南阳oj 366 全排列 D的小L
    南阳oj 32 组合数
    部分和问题 南阳oj 1058
    HNUSTOJ 1516:Loky的烦恼
    HDU-1874 畅通工程续
    T-聊天止于呵呵
    P-残缺的棋盘
  • 原文地址:https://www.cnblogs.com/gdwkong/p/7631748.html
Copyright © 2011-2022 走看看