練習 1.4:比較部署描述子的差異

開始之前,您應該先完成練習 1.3:比較 Java 類別的差異

在這個練習中,您將學會兩種 Portlet API 的部署描述子之間的差異。 請檢查 Portlet 部署描述子 (portlet.xml) 的兩個版本。 以下是範例中所說明的基本差異。 如果要編輯 Portlet 部署描述子,請使用 Portlet 部署描述子編輯器。

portlet.xml 的標示規則

IBM Portlet API 的標示規則由 DTD 來定義;JSR 168 Portlet API 則由 XML 綱目來定義。 這需要在 Portlet 部署描述子的頂端有不同的 XML 定義陳述式。

IBM Portlet API
<!DOCTYPE portlet-app-def PUBLIC "-//IBM//DTD Portlet Application 1.1//EN"
          "portlet_1.1.dtd ">
JSR 168 Portlet API
<portlet-app xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd"
             version="1.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd"
             xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd"
             id="bookmark_03_jsr.1">

<portlet-app> 元素的 id 屬性

兩個 API 在 <portlet-app> 元素的 id 屬性上使用不同的名稱。IBM Portlet API 使用 uid,JSR 168 Portlet API 使用 id。

IBM Portlet API
<portlet-app uid="com.ibm.etools.portal.portletexamples.bookmark.legacy"
JSR 168 Portlet API
<portlet-app xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd"
             version="1.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance "
             xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd "
             xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd"
             id="bookmark_03_jsr.1">

<portlet> 元素的 href 屬性

當使用 IBM Portlet API 時,<portlet> 元素的 href 屬性必須指向 Web 部署描述子 (web.xml) 中之 <servlet> 元素的對應 id; 當使用 JSR 168 Portlet API 時,<portlet-app> 元素的 id 屬性用來使伺服器唯一識別 Portlet。JSR 168 Portlet 不是 Servlet,不需要參照 web.xml。

IBM Portlet API
portlet.xml: <portlet id="Bookmark" href="WEB-INF/web.xml#Servlet_1086938566718"
web.xml:     <servlet id="Servlet_1086938566718">
JSR 168 Portlet API
<portlet-app xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd"
             version="1.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance "
             xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd "
             xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd"
             id="bookmark_03_jsr.1">

Portlet 版本

如果是 IBM Portlet API,請使用 <portlet-app> 和 <portlet> 元素的 major-version 和 minor-version 屬性; 如果是 JSR 168 Portlet API,請使用 <portlet-app> 元素的 version 屬性。

IBM Portlet API
<portlet-app uid="com.ibm.etools.portal.portletexamples.bookmark.legacy"
             major-version="1" minor-version="0">
<portlet id="Bookmark" href="WEB-INF/web.xml#Servlet_1086938566718"
             major-version="1" minor-version="0">
JSR 168 Portlet API
<portlet-app xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd"
             version="1.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance "
             xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd "
             xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd"
             id="bookmark_03_jsr.1">

支援的標記類型

在 Portlet 部署描述子中,IBM Portlet API 會宣告支援的標記類型,JSR 168 Portlet API 則會宣告支援的 MIME 類型。IBM 提供了延伸規格 wps.markup 起始設定參數,可讓您定義支援的 JSR 168 Portlet 標記類型。這用來區分使用相同 MIME 類型的標記類型,如 HTML 和 cHTML。

IBM Portlet API
<supports>
   <markup name="html">
      <view />
      <edit />
   </markup>
   <markup name="chtml">
      <view />
   </markup>
</supports>
JSR 168 Portlet API
<init-param>
   <name>wps.markup</name>
   <value>html,chtml</value>
</init-param>

<supports>
   <mime-type>text/html</mime-type>
   <portlet-mode>VIEW</portlet-mode>
   <portlet-mode>EDIT</portlet-mode>
</supports>

支援的模式

支援的模式必須定義在兩個 API 的 Portlet 部署描述子中,不過,語法有些不同。兩種 API 都支援編輯、檢視和說明模式。 IBM Portlet API 也支援配置模式。 JSR 168 Portlet API 支援自訂模式,如關於、預覽、列印、編輯預設值,以及配置。 兩種 API 都需要檢視模式;所有其他模式都是選用的。

IBM Portlet API
<supports>
   <markup name="html">
      <view />
      <edit />
   </markup>
</supports>
JSR 168 Portlet API
<supports>
   <portlet-mode>VIEW</portlet-mode>
   <portlet-mode>EDIT</portlet-mode>
</supports>

允許的視窗狀態

兩種 API 都自動會有正常狀態。 當使用 IBM Portlet API 時,必須明確宣告其他狀態,如 Solo。 當使用 JSR 168 Portlet API 時,也會自動提供放到最大和縮到最小狀態;自訂狀態必須明確宣告。

IBM Portlet API
<allows>
   <maximized/>
   <minimized/>
</allows>
JSR 168 Portlet API
範例中未顯示任何自訂狀態,但會使用 <custom-window-state> 元素

區域化

部分定義 Portlet 的區域化字串定義在 Portlet 部署描述子中。IBM Portlet API 的 portlet.xml 定義標題、短標題、說明和關鍵字的區域化值。IBM API 也有預設語言環境設定。JSR 168 Portlet API 在 portlet.xml 中定義 Portlet 說明和顯示名稱的區域化值。標題、短標題和關鍵字的值在 Portlet 資源軟體組中。如下所示,Portlet 資源軟體組 Portlet.properties 在範例的 nls 目錄中。JSR 168 API 有定義資源軟體組和支援的語言環境的設定。
IBM Portlet API
<default-locale>en</default-locale>
<language locale="zh-TW">
   <title>Bookmark portlet (IBM)</title>
   <title-short>Bookmark</title-short>
   <keywords>Bookmark</keywords>
</language>
<language locale="de">
   <title>Lesezeichen Portlet (IBM)</title>
   <title-short>Lesezeichen</title-short>
   <keywords>Lesezeichen, Bookmark</keywords>
</language>
JSR 168 Portlet API
<resource-bundle>nls.Portlet</resource-bundle>
<supported-locale>en</supported-locale>
<supported-locale>de</supported-locale>
<description xml:lang="EN">English description</description>
<display-name xml:lang="EN">English display name</display>-name>
<description xml:lang="DE">German description</description>
<display-name xml:lang="DE">German display name</display>-name>

範例 Portlet 資源軟體組 Portlet.properties:
javax.portlet.title = Bookmark Portlet
javax.portlet.short-title = Bookmark
javax.portlet.keywords = Bookmark

快取期限

在 IBM Portlet API 中,Portlet 可以利用 PortletRequest 物件的 invalidateCache() 方法,使快取明確失效 (失效型的快取) 。 預設快取期限和範圍設在 Portlet 部署描述子中。<expires> 元素的值必須是 0(一律到期)、-1(永不到期),或到期之前的秒數。no 的 <shared> 元素值表示 Portlet 實例不共用快取。

JSR Portlet API 使用期限型快取,快取期限時間定義在部署描述子中,但 Portlet 可以利用 RenderResponse 物件的 setAttribute() 方法和 EXPIRATION_CACHE 值來重設值。<expiration-cache> 元素使用 IBM Portlet API 在 <expires> 元素上所用的相同值。

IBM Portlet API
<cache>
   <expires>-1</expires>
   <shared>no</shared>
</cache>
JSR 168 Portlet API
<expiration-cache>0</expiration-cache>

現在您已準備好開始進行練習 1.5:比較 JSP 檔編碼的差異

讀者意見
(C) Copyright IBM Corporation 2000, 2005. All Rights Reserved.