zoukankan      html  css  js  c++  java
  • ⑦JSP2.0的福利(标签文件)

    前言

      本篇接着上一篇博客:[传送门]

      这次讲的是JSP2.0的特性之一,我们可以编写标签文件,指没有标签处理器和标签类库描述符的定制动作指令,不编译,无标签类描述符。

        本文结构:

          ①标签文件简介

          ②第一个尝试标签文件

          #重点:标签文件指令

          ③doBody

          ④invoke

    标签文件简介

      tag文件只是以tag为后缀名的文本文件。除了jsp页面指令外,其他JSP元素都可以出现在tag文件中页面引用格式。一,不需要编译,只要jsp语法即可,这意味不懂java的人也可以编写。二,不许需要面搜标签库描述符。

       格式:

      <%@ taglib prefix="test" tagdir="/WEB-INF/tags" %>

          tagdir:用于指定tag文件目录,当页面使用<ui:xxxx>进,会查找该目录下对应的xxxx.tag文件。

          prefix:指定使用时标签前缀

      使用:

    <test:xxxx/>

      

    第一个标签文件

      包结构图和效果图(将服务器当前时间显示):

                      

        感兴趣不?往下看吧

      firstTag.tag:

    <%@ tag import="java.util.Date" import="java.text.DateFormat"%>
    <%
        DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG);
        Date now = new Date(System.currentTimeMillis());
        out.println(dateFormat.format(now));
    %>

      firstTagTest.jsp

    <%@ taglib prefix="easy" tagdir="/WEB-INF/tags" %>
    Today is <easy:firstTag/>

     

    #重点:标签文件指令

      taglibincludeattributevariable

    1

    <%@ tag display-name="" body-content="" dynamic-attributes="" small-icon="" large-icon="" description="" example=""language="" import="" pageEncoding="" isELIgnored="">

     

      #tag 指令如同JSP网页的page指令,用来设定标签文件。

      #display-name表示图形化开发工具显示<display-name>所指定 的名称;

      #body-content表示可能的值有三种,分别是emptyscriptlesstagdependentempty

        empty为标 签中没有主体内容,

        scriptlet为标签中的主体内容ELJSP动作元素,但不可以为JSP脚本元素,

        tagdependent表示标签中的主体内 容交由tag自己去处理,默认值为scriptless

      #dynamic-attributes表示设定标签文件动态属性的名称,dynamic- attributes设定时,将会产生一个Map类型的集合对象,用来存放属性的名称和值;

      #small_icon表示在图形化开发工具显 示<small-icon>所指定的TLD相对路径的小图标,大小为16X16

      #large-icon表示在图形化开发工具显 示<large-icon>所指定的TLD相对路径的大图标,大小为32X32

      #description表示用来说明此标签文件的相关信 息;

      #example表示用来增加更多的标签使用说明,包括标签应用时的范例;

      #languageimportpageEncoding、 isELIgnored这些属性与page指令相对应的属性相同。

    2

    <%@ attribute name="" required="" fragment="" rtexprvalue="" type="" description=""%>

      这 个指令用来设定自定义标签的属性。其中name表示属性的名字;

      required表示是否为必要,默认为false

      rtexprvalue表示属性值是 否可以为run-time表达式。如为true,表示属性可用动态的方式来指定,如:<mytag:read num="${param.num}"/>,如为false,则一定要用静态的方式来指定属性值;

      type表示这个属性的类型,默认值为 java.lang.Stringdescription用来说明此属性的相关信息

    3

    <%@ variable name-given="" name-from-attribute="" alias="" variable-class="" declare="" scope="" desription="">

      这 个指令用来设定标签文件的变量。其中name-given表示直接指定变量的名称;

      name-from-attribute表示以自定义标签的某个属性值 为变量名称;

      alias表示声明一个局部范围属性,用来接收变量的值;variable-class表示变量的类名称,默认值为 java.lang.String

      declare表示此变量是否声明默认值为truescope表示此变量的范围,范围是:AT_BEGIN、 AT_ENDNESTED,默认值为NESTEDdescription用来说明此变量的相关信息

    ········variable案例引出 doBody

       jar结构 和 效果图

                

    doBodyDemo.tag

    <%@ tag import="java.util.Date" import="java.text.DateFormat"%>
    <%@ variable name-given="longDate" %>
    <%@ variable name-given="shortDate" %>
    <%
        Date now = new Date(System.currentTimeMillis());
        DateFormat longFormat = DateFormat.getDateInstance(DateFormat.LONG);
        DateFormat shortFormat = DateFormat.getDateInstance(DateFormat.SHORT);
        jspContext.setAttribute("longDate", longFormat.format(now));
        jspContext.setAttribute("shortDate", shortFormat.format(now));
    %>
    <jsp:doBody/>

    doBodyTest.jsp

    <%@ taglib prefix="easy" tagdir="/WEB-INF/tags" %>
    <easy:doBodyDemo>
        In Long format: ${longDate}
        <br/>
        In Short format: ${shortDate}
    </easy:doBodyDemo>

    新知识点-doBody-invoke

      <jsp:doBody>与<jsp:invoke>动作元素,前者是用来处理卷标本体文字,后者则是用来设 定标签间的Fragment,下面两个动作元素搭配着attribute与variable两个指令元素。

      

      首先介绍<jsp:doBody>您可以用它来决定是否显示本体文字,例如撰写如下的Tag File:

    check.tag

    <%@attribute name="password"%>
    <% if(password.equals("1234")) { %> 
    <jsp:doBody/> 
    <% } else { %> 
    密码不正确 
    <% } %> 

      必须先说明的是,这边使用了Scriptlet,这并不是一个良好的示范,若能搭配JSTL或自订标签库来完成Tag File的撰写会是比较好的,这边纯綷是为了说明方便才使用了Scriptlet。

      上面这个Tag File会检查传入的属性password是否符合我们设定的密码,如果符合就执行<jsp:doBody>,表示显示卷标之间的本体文字, 否则显示密码不正确的讯息,attribute指令元素可以指定自订卷标所使用的属性文字,可以使用下面的JSP网页来测试:

    test.jsp

    <%@taglib prefix="caterpillar" tagdir="/WEB-INF/tags/" %> 
    <html> 
    <body> 
    <caterpillar:check password="${ param.pwd }"> 
    您的秘密礼物在此! 
    </caterpillar:check> 
    </body> 
    </html> 

      您可以发现,Tag File即使是用来当作自订标签库的简便方式,也是十分的方便,不需要在tld档中作额外的设定,也可以得到相关的功能。

    attribute除了指定属性文字之外,也可以将属性当作Fragment传入,方便在Tag File中作个别的处理,例如下面撰写一个table.tag:

    table.tag

    <%@attribute name="frag1" fragment="true"%> 
    <%@attribute name="frag2" fragment="true"%>
    <table border="1"> 
    <tr> 
    <td><b>frag1</b></td> 
    <td><jsp:invoke fragment="frag1"/></td> 
    </tr> 
    <tr> 
    <td><b>frag2</b></td> 
    <td><jsp:invoke fragment="frag2"/></td> 
    </tr> 
    </table> 

    在这个Tag File中,将attribute的属性设定为Fragment,然后想取得指定的Fragment的话,就可以使用<jsp: invoke>动作元素,并指定Fragment的名称,使用下面这个JSP网页来测试:

    test.jsp

    <%@taglib prefix="caterpillar" tagdir="/WEB-INF/tags/" %> 
    <html> 
    <body> 
    <caterpillar:table> 
    <jsp:attribute name="frag1"> 
    Fragment 1 here 
    </jsp:attribute> 
    <jsp:attribute name="frag2"> 
    Fragment 2 here 
    </jsp:attribute> 
    </caterpillar:table> 
    </body> 
    </html> 

    在JSP网页中,同样的是使用<jsp:attribute>来说定Fragment的文字内容,执行这个JSP网页,会传回以下的内容:

    <html> 
    <body>
    <table border="1"> 
    <tr> 
    <td><b>frag1</b></td> 
    <td>Fragment 1 here</td> 
    </tr> 
    <tr> 
    <td><b>frag2</b></td> 
    <td>Fragment 2 here</td> 
    </tr> 
    </table> 
    </body> 
    </html> 

    在Tag File与JSP网页之间,可以使用variable指令元素设定Scripting Variable,以在两者之间传递变量内容,例如撰写以下的Tag File:

    precode.tag

    <%@attribute name="preserve" fragment="true" %> 
    <%@variable name-given="code" scope="NESTED" %>
    <jsp:doBody var="code" />
    <table border="1"> 
    <tr> 
    <td> 
    <pre><jsp:invoke fragment="preserve"/></pre> 
    </td> 
    </tr> 
    </table> 

    在这个Tag File中,使用variable设定Scripting Variable为"code",作用范围为"NESTED",也就是在起始卷标与结束卷标之间,而其中<jsp:doBody>中多了一项 属性var,表示在JSP网页中的<jsp:body>卷标中的文字内容将设定给"code"变量,可以用下面这个JSP网页来测试:

    test.jsp

    <%@ taglib prefix="caterpillar" tagdir="/WEB-INF/tags" %> 
    <html> 
    <body> 
    <caterpillar:precode> 
    <jsp:attribute name="preserve"> 
    <b>${ code }</b> 
    </jsp:attribute>
    <jsp:body> 
    PROGRAM MAIN 
    PRINT 'HELLO' 
    END 
    </jsp:body> 
    </caterpillar:precode> 
    </body> 
    </html> 

    <jsp:body>之间的虚拟程序代码将会传入给"code"变量,由于它是Scripting Variable,可以在标签之内起作用,所以在<jsp:attribute>中的EL式${code}可以取得Tag File中"code"的内容,也就是<jsp:body>传入的文字,之后我们将<jsp:attribute>的内容当作 Fragment在Tag File中作处理,结果将会如以下的网页:

    <html> 
    <body> 
    <table border="1"> 
    <tr> 
    <td> 
    <pre><b> 
    PROGRAM MAIN 
    PRINT 'HELLO' 
    END 
    </b></pre> 
    </td> 
    </tr> 
    </table> 
    </body> 
    </html>

    总结

        

       寄读者,寄知识来源

       读者,你好!你我不相识,谢谢你们支持。我的梦想会越来越接近。keep on,共勉!

       知识来源 http://book.douban.com/doulist/3575997/

  • 相关阅读:
    HDU 1850 Being a Good Boy in Spring Festival
    UESTC 1080 空心矩阵
    HDU 2491 Priest John's Busiest Day
    UVALive 6181
    ZOJ 2674 Strange Limit
    UVA 12532 Interval Product
    UESTC 1237 质因子分解
    UESTC 1014 Shot
    xe5 android listbox的 TMetropolisUIListBoxItem
    xe5 android tts(Text To Speech)
  • 原文地址:https://www.cnblogs.com/Alandre/p/3604176.html
Copyright © 2011-2022 走看看