国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JSP > 正文

利用JSP 2.0開發Web應用程序2

2024-09-05 00:20:29
字體:
來源:轉載
供稿:網友

標簽處理器

jsp 1.2中傳統的標簽處理api由于允許標簽體中包含scriptlet而變得復雜,但是現在利用表達式語言可以編寫不含scriptlet的jsp網頁。最終,jsp 2.0引入了一種新的標簽擴展機制,稱為“簡單標簽擴展”,這種機制有兩種使用方式:

  1. java開發人員可以定義實現接口javax.servlet.jsp.tagext.simpletag的類;
  2. 不懂java的網頁編寫人員則可以使用標簽文件。

首先來看第一種方式,代碼示例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>

要運行這個例子:

  1. 復制hellotg.java并保存到c:/tomcat5.0/webapps/jsp-examples/web-inf/classes/jsp2/examples/simpletag下;
  2. 使用javac編譯hellotag.java;
  3. 把代碼示例7中的標簽描述符添加到文件c:/tomcat5.0/webapps/jsp-examples/web-inf/jsp2/jsp2-example-taglib.tld中的</taglib>之前;
  4. 復制helloworld.jsp并保存到c:/tomcat5.0/webapps/jsp-examples/jsp2-tutorial目錄中;
  5. 用瀏覽器打開helloworld.jsp。

如果一切正常,應該會看到類似如圖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>

要運行這個例子:

  1. 復制標簽文件greetings.tags并保存在c:/tomcat5.0/webapps/jsp-examples/web-inf/tags 目錄下;
  2. 復制jsp網頁文件char.jsp并保存在c:/tomcat5.0/webapps/jsp-examples/jsp2-tutorial 目錄下;
  3. 用瀏覽器打開chat.jsp文件。

如果一切正常,應該會看到類似如圖5所示的窗口。


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>

要運行這個例子:

  1. 復制文件display.tag并保存在c:/tomcat5.0/webapps/jsp-examples/web-inf/tag 下;
  2. 復制文件newsportal.jsp并保存到c:/tomcat5.0/webapps/jsp-examples/jsp2-tutorial下;
  3. 用瀏覽器打開newsportal文件。

結果應該會得到與圖6類似的畫面。


6:把標簽文件用作模板

結論

jsp 2.0使得快速開發和維護動態網頁比以前更加容易,盡管“java”一詞出現在jsp中,但使用jsp2.0,網頁作者無須學習java程序語言本身,就能開發出全新的動態網頁。本文中的例子說明了使用jsp2.0的新特性開發動態網頁是多么容易。



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 湘阴县| 广水市| 安岳县| 北辰区| 廉江市| 新源县| 沅江市| 卢湾区| 剑河县| 炉霍县| 项城市| 焉耆| 平遥县| 兰州市| 磐石市| 卫辉市| 明水县| 罗定市| 平遥县| 红桥区| 莫力| 乌兰浩特市| 修水县| 桑日县| 汝阳县| 珲春市| 泊头市| 朝阳市| 长阳| 大足县| 贵溪市| 安宁市| 汉沽区| 祁门县| 盐山县| 嘉峪关市| 穆棱市| 丰镇市| 郯城县| 丰宁| 阳新县|