當頁面被返回給瀏覽器時,應用服務器HTML 模板語法使您能將變量字段放在 HTML 頁面上,并使 Servlet 和 JavaBean 可利用數據庫的值動態地替換變量。該功能是 JSP 的一個 IBM 擴展,它使引用變量數據變得十分容易。該語法只能用于 JSP 文件中。HTML 模板語法包括:
基本 HTML 模板語法;
替代 HTML 模板語法;
<REPEAT>標記。
這些標記通過 HTML 編寫工具被設計成傳遞交互操作的標記。每一個標記有一個相應的結束標記。每一個標記是分大小寫的,它們的一些屬性也是分大小寫的。IBM WebSphere Studio 使開發 JSP 文件以包含 HTML 模板語法變得更為容易。
(1) 基本 HTML 模板語法
<INSERT> 標記是用于指定變量字段的基本標記。一般的語法為:
<insert requestparm=pvalue requestattr=avalue bean=name
property=property_name(optional_index).subproperty_name(optional_index)
default=value_when_null>
</insert>
其中的屬性及其含義如下:
requestparm:要在請求對象內進行訪問的參數。該屬性是分大小寫的,并且不能與Bean 和property屬性一起使用。
Requestattr:要在請求對象內進行訪問的屬性。屬性應使用 setAttribute 方法設置。該屬性是分大小寫的,并且不能與 Bean 和property屬性一起使用。
Bean:由 <BEAN> 標記在 JSP 文件中說明的 JavaBean 名。請參閱訪問 JavaBean 以獲得 <BEAN> 標記的解釋。該屬性的值是分大小寫的。當指定了 Bean 屬性,但未指定property屬性時,在替換中將使用完整的 Bean 。例如,如果 Bean 是類型 String 且未指定屬性,則將替代 string 的值。
Property:訪問替換的 Bean 的屬性。該屬性的值是分大小寫的,并且是屬性的獨立場所名。該屬性不能與 requestparm 屬性一起使用。
Default:當 Bean 屬性值為空時,顯示可選的字符串。如果字符串包含多個字,則該字符串必須包含在一對雙引號中(例如 "HelpDesk number")。該屬性的值是分大小寫的。如果未指定值,則當屬性值為空時,用空字符串替代。
基本語法的示例如下:
<insert bean=userProfile property=username></insert>
<insert requestparm=company default="IBM Corporation"></insert>
<insert requestattr=ceo default="Company CEO"></insert>
<insert bean=userProfile property=lastconnectiondate.month></insert>
在大多數情況下,property屬性的值就是屬性名。但是,可以通過指定property屬性的全格式來訪問屬性的某一屬性(子屬性)。這個全格式也提供選擇項給您來指定索引屬性的一個索引。該可選的索引可以是一個常數(例如 2)或如重復設置 HTML 標記中描述的索引。使用屬性標記的全格式示例如下:
<insert bean=staffQuery property=address(currentAddressIndex)></insert>
<insert bean=shoppingCart property=items(4).price></insert>
<insert bean=fooBean property=foo(2).bat(3).boo.far></insert>
(2) 替代 HTML 模板語法
HTML 標準不允許在 HTML 標記中嵌入 HTML 標記。因而,就無法在另一個 HTML 標記中嵌入<INSERT>標記。作為代替,請使用 HTML 模板替代語法。要使用替代語法:
請使用<INSERT>和</INSERT>來包含 HTML 標記,在該標記中指出替代內容。
指定 Bean 和property屬性:
要指定 Bean 和屬性特性,請使用下列格式:$(bean=b property=p default=d),其中 b、p 和 d
作為描述基本語法的值。
要指定 requestparm 屬性,請使用下列格式:$(requestparm=r default=d),其中 r 和 d 作為
描述基本語法的值。
要指定 requestattr 屬性,請使用下列格式:$(requestattr=r default=d),其中 r 和 d 作為描述
基本語法的值。
替代 HTML 模板語法的示例如下:
<insert>
<img src=$(bean=productAds property=sale default=default.gif)>
</insert>
<insert>
<a href="http://www.myserver.com/map/showmap.cgi?country=$(requestparm=country default=
usa )
&city$(requestparm=city default="Research Triangle Park")&email=
$(bean=userInfo property=email)>Show map of city</a>
</insert>
(3) <REPEAT>標記
<REPEAT>標記的語法為:
<repeat index=name start=starting_index end=ending_index>
</repeat>
其中:
index:是用于標識該重復數據塊的一個可選的名稱。該值是分大小寫的。
Start:是用于該重復數據塊的一個可選的開始索引值。缺省值為 0 。
End:是用于該重復數據塊的一個可選的結束索引值。最大值是 2,147,483,647。如果結束屬性的值小于開始屬性的值,則忽略結束屬性。
下面的示例 1、2 和 3 顯示了如何使用<REPEAT>標記。如果所有的索引屬性擁有 300 個或更少的元素,則這些示例將產生相同的輸出。如果擁有的元素多于 300 個,示例 1 和示例 2 將顯示所有的元素,而示例 3 將只顯示前 300 個元素。示例 1 用缺省開始和結束索引顯示了隱式索引:使用最小索引屬性數的 bean 限制了循環重復的次數。
<table>
<repeat>
<tr><td><insert bean=serviceLocationsQuery property=city></insert></tr></td>
<tr><td><insert bean=serviceLocationsQuery property=address></insert></tr></td>
<tr><td><insert bean=serviceLocationsQuery property=telephone></insert></tr></td>
</repeat>
</table>
示例 2 顯示了索引、開始索引和結束索引:
<table>
<repeat index=myIndex start=0 end=2147483647>
<tr><td><insert bean=serviceLocationsQuery property=city(myIndex)></insert></tr></td>
<tr><td><insert bean=serviceLocationsQuery property=address(myIndex)></insert></tr>
</td>
<tr><td><insert bean=serviceLocationsQuery property=telephone(myIndex)></insert></tr>
</td>
</repeat>
</table>
示例 3 用隱式開始索引顯示了顯式索引和結束索引。雖然指定了索引屬性,仍可對索引過的屬性城市進行隱式索引,因為不需要(i)。
<table>
<repeat index=myIndex end=299>
<tr><td><insert bean=serviceLocationsQuery property=city></insert></tr></td>
<tr><td><insert bean=serviceLocationsQuery property=address(myIndex)></insert></tr>
</td>
<tr><td><insert bean=serviceLocationsQuery property=telephone(myIndex)></insert></tr>
</td>
</repeat>
</table>
可以嵌套<REPEAT>數據塊。獨立索引每個數據塊。該能力對交叉兩個 bean 上的交錯屬性或含有子屬性的屬性非常有用。在示例中,將兩個<REPEAT>數據塊嵌套,用以顯示用戶購物手推車上每一張壓縮光盤上的歌曲列表。
<repeat index=cdindex>
<h1><insert bean=shoppingCart property=cds.title></insert></h1>
<table>
<repeat>
<tr><td><insert bean=shoppingCart property=cds(cdindex).playlist></insert>
</td></tr>
</table>
</repeat>
</repeat>
新聞熱點
疑難解答