jsp 1.2中傳統的標簽處理api由于允許標簽體中包含scriptlet而變得復雜,但是現在利用表達式語言可以編寫不含scriptlet的jsp網頁。最終,jsp 2.0引入了一種新的標簽擴展機制,稱為“簡單標簽擴展”,這種機制有兩種使用方式:
首先來看第一種方式,代碼示例6給出了一個簡單的標簽處理器,它的作用僅僅是打印“this is my first tag! ”。
代碼示例6: hellotag.java
package jsp2.examples.simpletag;
import javax.servlet.jsp.jspexception;
import javax.servlet.jsp.tagext.simpletagsupport;import java.io.ioexception;
/**
* simpletag handler that prints "this is my first tag!"
*/
public class hellotag extends simpletagsupport {
public void dotag() throws jspexception, ioexception {
getjspcontext().getout().write("this is my first tag!");
}
}
編譯成功后,下一步就要在tld中定義一個標簽描述符,下面是標簽描述符的例子。
代碼示例7: 標簽描述符
<tag>
<description>prints this is my first tag</description>
<name>hello</name>
<tag-class>jsp2.examples.simpletag.hellotag</tag-class>
<body-content>empty</body-content>
</tag>
最后再編寫一個使用上述標簽的jsp頁面文件,見代碼示例8。
代碼示例8: helloworld.jsp
<%@ taglib prefix="mytag" uri="/web-inf/jsp2/jsp2-example-taglib.tld" %>
<html>
<head>
<title>simple tag handler</title>
</head>
<body>
<h2>simple tag handler</h2>
<p>
<b>my first tag prints</b>: <mytag:hello/>
</body>
</html>
要運行這個例子:
如果一切正常,應該會看到類似如圖4所示的畫面。
圖4:簡單標簽處理器
使用簡單標簽擴展機制的另一種方法是通過標簽文件。標簽文件是一種資源文件,網頁作者可以利用它抽取一段jsp代碼,通過定制功能來實現代碼的復用。換句話說,標簽文件允許jsp網頁作者使用jsp語法創建可復用的標簽庫。標簽文件的擴展名必須是“.tag”。
為說明使用標簽文件是多么容易,考慮一下代碼示例9。沒錯,這就是一個標簽文件!
代碼示例9: greetings.tag
hello there. how are you doing?
一旦定義了標簽文件,就可以在jsp網頁的編寫中使用這種定制的功能。比如代碼示例10中的jsp網頁。
代碼示例10: chat.jsp
<%@ taglib prefix="tags" tagdir="/web-inf/tags" %>
<html>
<head>
<title>jsp 2.0 examples - hello world using a tag file</title>
</head>
<body>
<h2>tag file example</h2>
<p>
<b>the output of my first tag file is</b>: <tags:greetings/>
</body>
</html>
要運行這個例子:
圖5:簡單的標簽文件
注意: 重要的是要注意到這里沒有為問候標簽編寫tld,而是創建了一個標簽文件并放在特殊的目錄中,然后用taglib指令導入并直接使用它。
標簽文件可以作為模板使用。考慮代碼示例11中的標簽文件display.tag,這個例子是根據tomcat 5.0中的面板的例子改寫的。指令attribute類似于tld中的<attribute>元素,允許聲明自定義的動作屬性。
代碼示例11: display.tag
<%@ attribute name="color" %>
<%@ attribute name="bgcolor" %>
<%@ attribute name="title" %>
<table border="0" bgcolor="${color}"> <tr>
<td><b>${title}</b></td>
</tr>
<tr>
<td bgcolor="${bgcolor}">
<jsp:dobody/>
</td>
</tr>
</table>
代碼示例12給出了使用上述標簽的一個簡單的jsp頁面。
代碼示例12: newsportal.jsp
<%@ taglib prefix="tags" tagdir="/web-inf/tags" %>
<html>
<head>
<title>another tag file example</title>
</head>
<body>
<h2>news portal: another tag file example</h2>
<table border="0"> <tr valign="top">
<td>
<tags:display color="#ff0000" bgcolor="#ffc0c0"
title="travel">
last french concorde arrives in ny<br/>
another travel headline<br/>
yet another travel headline<br/>
</tags:display>
</td>
<td>
<tags:display color="#00fc00" bgcolor="#c0ffc0"
title="technology">
java for in-flight entertainment<br>
another technology headline<br>
another technology headline<br>
</tags:display>
</td>
<td>
<tags:display color="#ffcc11" bgcolor="#ffffcc"
title="sports">
american football<br/>
nba<br/>
soccer<br/>
</tags:display>
</td>
</tr>
</table>
</body>
</html>
要運行這個例子:
結果應該會得到與圖6類似的畫面。
圖6:把標簽文件用作模板
jsp 2.0使得快速開發和維護動態網頁比以前更加容易,盡管“java”一詞出現在jsp中,但使用jsp2.0,網頁作者無須學習java程序語言本身,就能開發出全新的動態網頁。本文中的例子說明了使用jsp2.0的新特性開發動態網頁是多么容易。
新聞熱點
疑難解答