jsp2.0+中的標籤檔案,JSP Fragment技術

來源:互聯網
上載者:User

標籤:res   encoding   一個   import   官方   ace   lib   2.0   expr   

剛進新公司不久,今天在看到項目中用到了.tag檔案。剛開始我還以為這個是第三方類似freemarker的模板技術。問了下項目組的其它人員,原來這是jsp2.0以來就有的JSP Fragment技術。曾經做項目的時候從來沒實用這種方式,要公用就用用jsp中的include和jsp:include的方式。事實上JSP Fragment也有include的作用,可是它更像第三方sitemesh技術。用於網頁布局和修飾,能夠將網頁的內容和頁面的結構分離。從而達到頁面結構共用的目的。

以下的範例來說明怎麼使用jsp fragment。

官方E文參考文檔http://docs.oracle.com/javaee/5/tutorial/doc/bnama.html

DEMO

1 首先在項目的WEB-INF/tags檔案裡,建立例如以下內容的tpl.tag檔案

<%@ tag language="java" pageEncoding="UTF-8"%><%@ attribute name="title"%><%@ attribute name="tpl1" fragment="true" required="true"%><%@ attribute name="tpl2" fragment="true" required="true"%><%@ attribute name="tpl3" fragment="true" required="true"%><!DOCTYPE html><html><head><meta charset="utf-8"><title>${title}</title><style type="text/css">   #div1,#div2,#div3{      width:90%;      margin:10px auto;      border:10px solid red;   }</style></head><body>    <h1>jsp2.0標籤檔案</h1>    <div id="div1">    <jsp:invoke fragment="tpl1"/>    </div>    <div id="div2">    <jsp:invoke fragment="tpl2"/>    </div>    <div id="div3">    <jsp:invoke fragment="tpl3"/>    </div>    <h2>jsp2.0 fragment技術</h2></body></html>

2  建立index.jsp 檔案

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ taglib prefix="xjo" tagdir="/WEB-INF/tags"%><xjo:tpl title="jsp標籤檔案的使用"><jsp:attribute name="tpl1"><h1>tpl1中的內容</h1></jsp:attribute><jsp:attribute name="tpl2"><h1>tpl2中的內容</h1></jsp:attribute><jsp:attribute name="tpl3"><h1>tpl3中的內容</h1></jsp:attribute></xjo:tpl>

3 訪問index.jsp頁面。效果例如以下


詳細說明標籤檔案說明

標籤檔案必須存放在WEB-INF檔案夾下,最好是在WEB-INF檔案夾下再建立一個tags檔案夾,然後將全部的標籤檔案都存放在這裡。標籤檔案能夠使用全部的JSP元素,可是不能使用page指令。標籤檔案多個一個tag指令

標籤檔案裡隱藏對象標籤檔案能夠使用隱藏對象:request、response、jspContext、session、application、out、config。當中jspContext隱含對象的類型為javax.servlet.jsp.JspContext,就相當於JSP頁面的pageContext隱藏對象一樣
標籤檔案裡指令標籤檔案裡能夠使用的主要指令:tag、include、attribute、variable。



tag指令Tag檔案裡的tag指令類似於JSP檔案裡的page指令。Tag檔案通過使用tag指令能夠指定某些屬性的值,以便從整體上影響Tag檔案的處理和表示。tag指令的文法例如以下:
<%@ tag 屬性1="屬性值" 屬性2="屬性值" …屬性n="屬性值"%>
在一個Tag檔案裡能夠使用多個tag指令,因此我們常常使用多個tag指令為屬性指定須要的值:
 <%@ tag 屬性1="屬性值"%>
 <%@ tag 屬性2="屬性
 值"%> ……
 <%@ tag 屬性n="屬性值"%>

<%@ tag language="java"         pageEncoding=""        body-content="scriptless|empty|tagdependent"        deferredSyntaxAllowedAsLiteral="false"        description=""        display-name=""        dynamic-attributes=""        example=""        import=""        isELIgnored="false"        large-icon=""        small-icon=""        trimDirectiveWhitespaces="false"%>

include 指令和jsp中的include指定一樣,引入外部檔案
<%@include file="" %>

attribute指令在Tag檔案裡通過使用attribute指令,能夠動態地向該Tag檔案傳遞須要的字串資料
<%@ attribute name=""               description=""               fragment="false"               required="false"                rtexprvalue="true"               type=""%>
name表示屬性的名字;required表示是否為必要,默覺得false;rtexprvalue表示屬性值能否夠為run-time運算式。如為true。表示屬性可用動態方式來指定。如為false,則一定要用靜態方式來指定屬性值;type表示這個屬性的類型,預設值為java.lang.String;description用來說明此屬性的相關資訊
fragment  表示是否為片段 預設false
variable指令Tag檔案通過使用variable指令能夠將Tag檔案裡的對象返回給調用該Tag檔案的JSP頁面
<%@ variable alias=""              declare="true"              description=""             name-from-attribute=""             name-given=""              scope="NESTED|AT_BEGIN|AT_END"%>
name-given表示直接指定變數的名稱。name-from-attribute表示以自己定義標籤的某個屬性值為變數名稱;alias表示聲明一個局部範圍屬性,用來接收變數的值;variable-class表示變數的類名稱,預設值為java.lang.String;declare表示此變數是否聲明預設值為true。scope表示此變數的範圍,範圍是:AT_BEGIN、AT_END和NESTED。預設值為NESTED;description用來說明此變數的相關資訊
標籤檔案裡的jsp:invoke
<jsp:invoke fragment=""     var=""     scope="page|request|session|application"     varReader=""/>
    fragment  -  要片段名.
   var  -  給出變數名,  把片段經過 jsp 容器計算過之後的結果作為字串儲存. 
   varReader  -  同上,  只是將結果作為一個 java.io.Reader 儲存. 
   scope  -  範圍 
未完待續.......................

jsp2.0+中的標籤檔案,JSP Fragment技術

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.