1轉自:https://blog.csdn.net/yusimiao/article/details/46835617
Jsp自定義tag標簽
自定義tag標簽的好處
程序員可以自定一些特定功能的標記, 用來封裝代碼, 達到分工, 重用性等多種好處.
如何存放tag標簽
通常在web工程WEB-INF文件夾下創建tags文件夾來存放自定義的tag,如/WEB-INF/tags
tag標簽的語法
要知道怎樣定義tag標簽就需要知道tag標簽的基本屬性,例如:
<%@ tag body-content="empty" trimDirectiveWhitespaces="true" pageEncoding="UTF-8"%>
通常我們在*.tag文件的開頭加上以上的代碼來告訴jsp容器這是一個tag標記文件。
body-content有以下三種屬性:
1. empty:這個是一個空標記.
2. scriptless:標記主體可以有內容, 並且jsp容器會去處理里面的jsp元素, 這些內容可以是文本, EL表達式, 標准動作甚至另一個自定義標記.
3. tagdependent:標記主體可以有內容, 而jsp容器會把它們當作純文件處理
trimDirectiveWhitespaces=“true”表示刪除多余的空行
pageEncoding=”UTF-8” 表示page的編碼格式
標記中也可以定義attribute,例如:
<%@ attribute name="x" required="true" rtexprvalue="true" %> <%@ attribute name="y" required="true" rtexprvalue="true" %>
attribute的屬性介紹如下:
1. name :這個attribute的名稱.
2. required : true/false, 是否必須的.
3. rtexprvalue : true/false, 這個attribute可否使用EL表達式, 否則為純文本.
4. type : 設定這個attribute的類型, jsp容器會把結果自動轉換成這個類.
單純的說以上的定義是不是有點糊塗呢?農夫選豆種——舉例(粒)為證:
先展示出自定義的tag在web工程下的目錄結構,
我們看一下index.jsp的代碼:
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 4 <%@ taglib tagdir="/WEB-INF/tags/" prefix="yu" %> 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 6 <html> 7 <head> 8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 9 <title>define tag</title> 10 </head> 11 <body> 12 <!-- test tag [body-content="empty"] --> 13 <yu:add x="1" y="2"/> 14 15 </body> 16 </html>
該index.jsp中引用了add.tag文件,看一下add.tag文件的定義:
1 <%@ tag body-content="empty" trimDirectiveWhitespaces="true" pageEncoding="UTF-8"%> 2 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 3 <%@ attribute name="x" required="true" rtexprvalue="true" %> 4 <%@ attribute name="y" required="true" rtexprvalue="true" %> 5 <div> 6 <h4>The result of x+y is</h4> 7 result = ${x+y} 8 </div>
運行起來程序我們看看運行結果:
至此,基本上已經理解了tag文件的使用了吧,那我們做進一步的學習。
繼續編寫index.jsp代碼,來驗證body-content=”scriptless”的tag文件:
1 <!-- test tag [body-content="scriptless"] --> 2 <yu:scriptless_tag> 3 This tag's body-content is [scriptless], let's have a test, i'm here.<br/> 4 <label>contextPath:<label/> ${pageContext.request.contextPath} 5 </yu:scriptless_tag>
那我們來看一下scriptless_tag.tag文件的定義:
1 <%@ tag body-content="scriptless" trimDirectiveWhitespaces="true" pageEncoding="UTF-8"%> 2 <div style="background-color: red; width=auto">my backgroud color is red</div> 3 <jsp:doBody></jsp:doBody> 4 <div style="background-color: blue; width=70px;">my backgroud color is blue</div>
細心的朋友發現這里面多了一個\<\jsp:doBody>標簽,該標簽的作用就是講index.jsp中該標記文件標記的主體顯示在該處,來看一下程序運行結果吧,一目了然:
在index.jsp文件中scriptless_tag標簽標記的主體顯示在了紅色背景和藍色背景之間,並且將代碼${pageContext.request.contextPath}解析成了 /yuTestTag
繼續編寫index.jsp代碼,來驗證body-content=”tagindependent”的tag文件:
1 <!-- test tag [body-content="tagindependent"] --> 2 <yu:tagdependent_tag> 3 This tag's body-content is [tagindependent], let's have a test, i'm here. 4 <label>contextPath:<label/> ${pageContext.request.contextPath} 5 </yu:tagdependent_tag>
scriptless_tag.tag文件的定義和scriptless_tag.tag的內容基本一樣:
1 <%@ tag body-content="tagdependent" trimDirectiveWhitespaces="true" language="java" pageEncoding="UTF-8"%> 2 <div style="background-color: red; width=auto">my backgroud color is red</div> 3 <jsp:doBody></jsp:doBody> 4 <div style="background-color: blue; width=70px;">my backgroud color is blue</div>
繼續看看運行結果:
代碼${pageContext.request.contextPath}沒有被解析,只是當做純文本文件
最后附上index.jsp的完整代碼:
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 4 <%@ taglib tagdir="/WEB-INF/tags/" prefix="yu" %> 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 6 <html> 7 <head> 8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 9 <title>define tag</title> 10 </head> 11 <body> 12 <!-- test tag [body-content="empty"] --> 13 <yu:add x="1" y="2"/> 14 15 <hr> 16 17 <!-- test tag [body-content="scriptless"] --> 18 <yu:scriptless_tag> 19 This tag's body-content is [scriptless], let's have a test, i'm here.<br/> 20 <label>contextPath:<label/> ${pageContext.request.contextPath} 21 </yu:scriptless_tag> 22 23 <hr> 24 25 <!-- test tag [body-content="tagindependent"] --> 26 <yu:tagdependent_tag> 27 This tag's body-content is [tagindependent], let's have a test, i'm here. 28 <label>contextPath:<label/> ${pageContext.request.contextPath} 29 </yu:tagdependent_tag> 30 </body> 31 </html>
到此我相信這三種類型的tag的區別已經顯而易見了,先簡單的寫到這,如有不妥請留言補充說明。