每個 portlet 應(yīng)用程序需要兩個部署描述符:
•portlet 部署描述符為門戶網(wǎng)站服務(wù)器提供有關(guān)應(yīng)用程序中 portlet 資源的信息,包含配置、支持特征和本地化的標(biāo)題。門戶網(wǎng)站服務(wù)器使用該信息為 portlet 提供服務(wù)。例如,如果 portlet 在 portlet 部署描述符中注冊它對幫助和編輯方式的支持,則門戶網(wǎng)站服務(wù)器將呈現(xiàn)圖標(biāo)以允許用戶調(diào)用 portlet 的幫助和編輯頁面。此文件始終名為 portlet.xml。
•Web 應(yīng)用程序描述符為應(yīng)用程序服務(wù)器提供有關(guān)應(yīng)用程序中 Web 資源的信息。此文件始終名為 web.xml。此文件的格式在 Servlet 規(guī)范 2.3 的“部署描述符”一章中進(jìn)行了描述。
Web 應(yīng)用程序部署描述符
本部分描述 IBM portlet 和遵從 JSR 168 的 portlet 的 web.xml 的必需元素。要獲取有關(guān) Web 應(yīng)用程序部署描述符的更多信息,請參閱 Java Servlet 規(guī)范 V2.3。
JSR 168 Web 應(yīng)用程序描述符
由于 web.xml 是 J2EE Web 歸檔文件中的一個必需項,所以您應(yīng)確保您的 portlet 應(yīng)用程序在它的 WAR 文件中包含 web.xml。這確保 portlet 應(yīng)用程序與其他門戶網(wǎng)站服務(wù)器實(shí)施以及 WebSphere Portal 的后繼版本兼容。web.xml 應(yīng)該至少包含 <web-app/>、 <display-name/> 和 <tag-lib/> 元素。您還可使用 <init-param> 元素包含上下文范圍的參數(shù)。請參閱 Java Servlet 規(guī)范,版本 2.3,以獲取有關(guān) Web 應(yīng)用程序部署描述符結(jié)構(gòu)的更多信息。
<tag-lib/> 必須表明門戶網(wǎng)站服務(wù)器上 JSR 168 JSP 標(biāo)記庫的位置,如下所示:
<taglib id="PortletTLD">
<taglib-uri>http://java.sun.com/portlet</taglib-uri>
<taglib-location>tld/std-portlet.tld</taglib-location>
</taglib>
注:如果缺少 <taglib/> 元素,當(dāng)使用門戶網(wǎng)站管理或 XML 配置界面部署 JSR 168 portlet 應(yīng)用程序 WAR 文件時,會在 web.xml 中自動插入它。然而,不更新使用預(yù)部署方式安裝的 JSR 168 EAR 文件的 web.xml,所以需要包含 <taglib/> 元素。
IBM Web 應(yīng)用程序描述符
與其他遵從 J2EE 模型的 servlet 一樣,IBM portlet 和 Web 應(yīng)用程序部署描述符(web.xml)一起被打包為 WAR 或 EAR 文件。該描述符將每個 portlet 定義為 Web 應(yīng)用程序中的 servlet,包含每個 portlet 唯一的標(biāo)識、portlet 類和初始化參數(shù)。
web.xml 中 servlet 的定義必須與 portlet.xml 中 portlet 的定義有相同的順序。servlet 標(biāo)識必須由 portlet 部署描述符使用 <portlet> 標(biāo)記的 href 屬性來引用。如下表所示,href 屬性把由 servlet 標(biāo)識追加的 WAR 文件中 Web 應(yīng)用程序描述符的路徑表示為錨。
portlet.xml web.xml
<portlet id="Portlet_1"
href="WEB-INF/web.xml#Servlet_1">
<portlet-name>Mail</portlet-name>
...
</portlet>
<servlet
id="Servlet_1">
<servlet-name>MailPortlet</servlet-name>
...
</servlet>
<portlet id="Portlet_2"
href="WEB-INF/web.xml#Servlet_2">
<portlet-name>Calendar</portlet-name>
...
</portlet>
<servlet
id="Servlet_2">
<servlet-name>CalendarPortlet</servlet-name>
...
</servlet>
Portlet 部署描述符
本部分描述 IBM portlet 和遵從 JSR 168 的 portlet 的 portlet.xml。
•IBM portlet.xml 的元素和結(jié)構(gòu)在本主題中提供。
•要獲取有關(guān) JSR 168 的 portlet 部署描述符的信息,請參閱 JSR 168 Portlet 規(guī)范。
portlet 應(yīng)用程序標(biāo)識的準(zhǔn)則
portlet 應(yīng)用程序的標(biāo)識必須在它們的使用區(qū)域(可能是全球范圍)中清楚地標(biāo)識它們。對于 IBM portlet 的具體 portlet 應(yīng)用程序也是這樣的。為了實(shí)現(xiàn)上述目的,請遵循這些準(zhǔn)則。
•使用與用于 Java 包相同的格式把 portlet 的名稱空間包含在標(biāo)識中
•添加一些 portlet 應(yīng)用程序特定的描述
•添加一些任意的字符以確保名稱空間中的唯一性,例如:
com.ibm.wps.samplet.mail.4969
•對于 IBM portlet,添加相應(yīng)具體 portlet 應(yīng)用程序的后綴,例如:
com.ibm.wps.samplet.mail.4969.1
Portlet 標(biāo)識在應(yīng)用程序中必須是唯一的。
JSR 168 portlet 部署描述符
在 JSR 168 中,portlet 描述符的格式為 XML Schema。因?yàn)?portlet 不是一個 servlet,所以 portlet 描述符不引用 web.xml 中相應(yīng)的 servlet 標(biāo)識。頂層元素是 <portlet-app>,它包含一個或多個 <portlet> 定義。特定于 Portlet 的初始化參數(shù)使用 <init-param> 元素存儲在 portlet.xml 中,并從初始化期間提供的 PortletConfig 對象獲取。
IBM portlet 部署描述符
IBM portlet 部署描述符的結(jié)構(gòu)由 DTD 定義,它位于 wps.war/dtd/portlet_1.1.dtd。單個 portlet 應(yīng)用程序的描述符以下列 XML 和 DOCTYPE 聲明開頭:
<?xml version="1.0"?>
<!DOCTYPE portlet-app-def PUBLIC "-//IBM//DTD Portlet Application 1.1//EN" "portlet_1.1.dtd">
下面顯示 IBM portlet 的部署描述符(portlet.xml)的結(jié)構(gòu)。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE portlet-app-def PUBLIC "-//IBM//DTD Portlet Application 1.1//EN" "portlet_1.1.dtd">
<portlet-app-def>
<portlet-app uid="uid">
<portlet-app-name>portlet_application_name</portlet-app-name>
<portlet id="portlet_id" href="WEB-INF/web.xml#servlet_id"
major-version="version" minor-version="version">
<portlet-name>portlet_name</portlet-name>
<cache>
<expires>number</expires>
<shared>yes|no</shared>
</cache>
<allows>
<maximized/>
<minimized/>
</allows>
<supports>
<markup name="html|wml|chtml">
<view output="fragment"/>
<edit output="fragment"/>
<help output="fragment"/>
<configure output="fragment"/>
</markup>
</supports>
</portlet>
</portlet-app>
<concrete-portlet-app uid="uid">
<portlet-app-name>portlet_application_name</portlet-app-name>
<context-param>
<param-name>name</param-name>
<param-value>value</param-value>
</context-param>
<concrete-portlet href="#portlet_id">
<portlet-name>portlet_name</portlet-name>
<default-locale>locale</default-locale>
<language locale="locale">
<title>title</title>
<title-short>short title</title-short>
<description>description</description>
<keywords>keyword1, keyword2</keywords>
</language>
<config-param>
<param-name>name</param-name>
<param-value>value</param-value>
</config-param>
</concrete-portlet>
</concrete-portlet-app>
</portlet-app-def>
鏈接 portlet 和具體 portlet
對于 IBM portlet,每個具體 portlet 定義使用 <concrete-portlet> 標(biāo)記的 href 屬性表明它的父 portlet。如下表所示,href 屬性表明 portlet 標(biāo)識為錨。
Portlet 標(biāo)記 具體 portlet 標(biāo)記
<portlet id="Portlet_1"
href="WEB-INF/web.xml#Servlet_1">
<portlet-name>Mail</portlet-name>
...
</portlet>
<concrete-portlet href="#Portlet_1">
<portlet-name>Mail Box</portlet-name>
...
</concrete-portlet>
<portlet id="Portlet_2"
href="WEB-INF/web.xml#Servlet_2">
<portlet-name>Calendar</portlet-name>
...
</portlet>
<concrete-portlet href="#Portlet_2">
<portlet-name>Group calendar</portlet-name>
...
</concrete-portlet>