使用Eclipse plus Pluto開發(fā)你的第一個與JSR168兼容的Portlet
By Terry.li
SpiritSeekerS@sqatester.com[/pre]
本文將介紹開發(fā)基于Portlet Specification v1.0 (JSR168) 的Portlet應(yīng)用.
我們使用Pluto作為Portlet Container和Portal. 同時可以將開發(fā)的Portlet應(yīng)用遷移到任何支持JSR168的Portlet Container上.
· 為什么要發(fā)布Portlet Specification? 什么是JSR168?
由于越來越多的公司開發(fā)了各自的Portal組件和基于其的Portal產(chǎn)品(如Bea, IBM, Oracle, Sun, Sybase, Novell, SAP, Jetspeed, Vignette 等.這種互不兼容的接口實現(xiàn)不斷帶給程序提供商各種問題和麻煩, 為了解決這種問題, JCP發(fā)布了JSP168 (Java Specification Request), Portlet Specification, 用以提供不同Portal和Portlets之間的互用性
· 什么是Portal?
Portal是基于WEB的應(yīng)用程序, 它將不同資源進(jìn)行整合并展現(xiàn)給用戶
通常其有如下三個特點:
a. Personalization (個性化)
b. Single sign on (單點登陸)
c. Content aggregation (內(nèi)容聚合)
其中Content aggregation指的是將不同來源的信息整合到一個頁面中用來讓用戶更方便的進(jìn)行使用.
比如, 如果某客戶需要進(jìn)行一次商業(yè)采購,以往需要訪問不同的供應(yīng)商的主頁得到相關(guān)信息, 但如果使用Portal將所有不同的貨物供應(yīng)商的商品catalog頁面都整合到一個Catalog Portal頁面中, 那么所有的商品信息都可以更快的進(jìn)行瀏覽,篩選和定貨, 加快了客戶的商業(yè)運作效率.
· 什么是Portlet?
Portlet是一種基于WEB組件的JAVA技術(shù), 由Portlet Container進(jìn)行管理. 處理請求并動態(tài)返回頁面, 可以做為Portal 的可拔插的用戶界面組件.
· 什么是Portlet Container? Portlet Container用來管理Portlet的生命周期并且提供其運行所需要的必要環(huán)境. 并且為Portlet Preferences提供持久性(Persistent)存取服務(wù).但是其不支持內(nèi)容的Aggregation. Aggregation由Portal組件提供.
注: Portlet Preferences是Portlet的一個新特性,提供類似數(shù)據(jù)庫的功能.但是不是用來取代數(shù)據(jù)庫. 只是用來存取簡單的Portlet參數(shù)配置.
· 什么是WSRP?
WSRP 是 OASIS Web Service for Remote Portlets的縮寫. WSRP主要用來簡化Portal對于各種資源或者程序的整合的復(fù)雜性, 可以避免編程帶來的整合的復(fù)雜性. 并且Portal的管理員可以從大量的服務(wù)中選擇需要的用以整和到Portal中.
· Portlet and Servlet
摘自(Portlet Specs v1.0)
相同點:
? Portlets are Java technology based web components
? Portlets are managed by a specialized container
? Portlets generate dynamic content
? Portlets lifecycle is managed by a container
? Portlets interact with web client via a request/response paradigm
不同點:
? Portlets only generate markup fragments, not complete documents. The Portal aggregates portlet markup fragments into a complete portal page
? Portlets are not directly bound to a URL
? Web clients interact with portlets through a portal system
? Portlets have a more refined request handling, action requests and render requests
? Portlets have predefined portlet modes and window states that indicate the function the portlet is performing and the amount of real state in the portal page
? Portlets can exist many times in a portal page
Portlet特有:
? Portlets have means for accessing and storing persistent configuration and customization data
? Portlets have access to user profile information
? Portlets have URL rewriting functions for creating hyperlinks within their content, which allow portal server agnostic creation of links and actions in page fragments
? Portlets can store transient data in the portlet session in two different scopes: the application-wide scope and the portlet private scope
Servlet特有:
? Setting the character set encoding of the response
? Setting HTTP headers on the response
? The URL of the client request to the portal
· 什么是Pluto ?
Pluto 是 Apache開發(fā)下的一個Open Source項目, 是基于Portlet Specs的一個 Portlet Container 的實現(xiàn). 它也提供了Sample Portal實現(xiàn). 但是功能相對簡單:
例如,
1) 沒有復(fù)雜的Layout實現(xiàn).
2) 不是multi-user enabled, 比如, 不同User之間的Portlet Preferences互相是可以share的.
請記住Pluto只是一個Portlet Container的實現(xiàn), 不是一個Portal的實現(xiàn). 如果你需要功能更為強大的Portal,可以使用JetSpeed , 它同樣也是Apache的一個Open Source Project. 請參考文章末尾的資源部分.
· 概念
如圖: Figure 1.1
a. Decorations and controls (修飾部分及 控制部分)
b. Portlet fragment (Portlet 片段)
c. Portlet window (Portlet 窗口)
d. Portlet page (Portlet 頁面)
Figure 1.1
· 開發(fā)工具( Eclipse2.1, Pluto-plugin, Jakarta-tomcat-4.1.29)
下載地址:
Eclipse
http://www.eclipse.org/downloads/index.php
Pluto-plugin
http://prdownloads.sourceforge.net/plutoeclipse/org.eclipsefan.pluto.ui_1.0.0.zip?download
Tomcat4.1
http://mirrors.midco.net/pub/apache.org/jakarta/tomcat-4/v4.1.29/bin/jakarta-tomcat-4.1.29.zip
JDK1.4
http://Java.sun.com
· 配置開發(fā)環(huán)境
Step1 : 解壓eclipse-SDK-2.1.1-win32.zip
Step2 : 解壓org.eclipsefan.pluto.ui_1.0.0.zip , 并將其拷入eclipse\plugins目錄
下, 如下: eclipse\plugins\org.eclipsefan.pluto.ui_1.0.0\
\Icon
\Lib
\Source
\Webapp
…
Step3: 解壓tomat4.1到一目錄. 并且配置好Tomcat服務(wù)器.
Congratulations, It’s done!
a. 使用Portlet Wizard 來創(chuàng)建Portal
1) 選擇File > New > Project… > Portal > Pluto Portal application, 單 擊Next
2) 指定Project名稱 , 這里使用pluto, 然后單擊Next.
3) 指定Tomcat路徑, 單擊Finish. 之后eclipse會將Pluto安裝到tomcat上,并且自動配置好環(huán)境.
b. 創(chuàng)建Portlets Application
1) 創(chuàng)建Pluto portlet application
2) 輸入project名稱,這里用portlets
c. 最后生成的工程如圖
d. 通過portlet.xml 生成/更新 web.xml
e. 部署 Portlets
1) 編譯 portlets 工程.
2) 在 [your tomcat installation directory]/webapps下, 新建目錄portlets
3) 將portlets工程下的web-root目錄下所有文件, 拷入以上新建的portlets目錄中.
Tip: 如果在每次修改后你厭倦了Ctrl+C 和 Ctrl+V, 你可以在Portlets工程目錄下自己編一個Bat文件, 然后你可以在eclipse里的Package Explorer中看到它, 雙擊就可以了. 其中deploy.bat文件就是實現(xiàn)其功能的.
4) Start tomcat.
5) 在IE 中輸入: http://localhost: 8080/pluto/portal, 你應(yīng)該能看到portal 頁面.
· Sample Portlet
在Portlets工程文件創(chuàng)建過程中, 自動生成許多文件, 包括一個簡單的Portlet, 我們來觀察一下生成的sample portlet.
1) 與Servlet非常類似, Portlet擴(kuò)展自GenericPortlet
import javax.portlet.*;
public class SimplePortlet extends GenericPortlet
2) 其三個方法對應(yīng)了Portlet 標(biāo)題欄中的三個聯(lián)接 (View, Edit, Help)
public void doView(…)
public void doEdit(…)
public void doHelp(…)
Figure 1.9
3) 三個方法分別調(diào)用了三個JSP文件, 用以生成Portlet fragment, 同樣也可以調(diào)用Servlet產(chǎn)生Portlet fragment. 與Servlet應(yīng)用類似,也可以使用
getInitParameter(String s)方法,得到配置文件中Portlet的初始值. 只不過Servlet使用web.xml,而Portlet 使用portlet.xml文件.
portlet.xml
<init-param>
<name>jspView</name>
<value>/jsp/view.jsp</value>
</init-param>
SimplePortlet.java
String jspName =
getPortletConfig().getInitParameter("jspView");
· Portlet Tag library
1)defineObjects Tag
如果打開其中的一個JSP文件,你會發(fā)現(xiàn),和Servlet一樣,JSP中可以使用Portlet的一些variables.例如:
renderResponse
renderRequest
portletConfig
但是必須聲名:
<portlet:defineObjects/>
其中不可以定義任何屬性或者包含任何內(nèi)容.
2) actionURL Tag
屬性 值類型 對應(yīng)值
windowState String minimized, normal, maximized
portletMode String view, edit, help
var String 任何值
secure String true,false
<portlet:actionURL windowState=”normal” portletMode=”edit”>
<portlet:param name=”action” value=”login”/>
</portlet:actionURL>
創(chuàng)建一個action URL , 當(dāng)訪問它時將使portlet window變?yōu)閚ormal 裝態(tài), 模式變?yōu)?nbsp;edit.
3) renderURL Tag
屬性 值類型 對應(yīng)值
windowState String minimized,normal,maximized
portletMode String view, edit, help
var String 任何值
secure String true,false
<portlet:renderURL portletMode=”view” windowState=”maximized”>
<portlet:param name=”number” value=”1”/>
<portlet:param name=”page” value=”2”/>
</portlet:renderURL>
創(chuàng)建一個render URL , 當(dāng)訪問它時將使portlet window變?yōu)閙aximized裝態(tài), 模式變?yōu)?nbsp;view.
4) namespace Tag
為目前的Portlet產(chǎn)生一個唯一的Value. 防止和其他的Portlet 或者Portal頁面上的value產(chǎn)生沖突.
<A HREF=”javascript: <portlet:namespace/>doFoo()”>Foo</A>
5) param Tag
屬性 值類型
name String
<portlet:param name=”myParam” value=”someValue”/>
注: param Tag不可以有body content.
總結(jié)Pluto目前是v1.0, 有部分Portlet規(guī)范沒有完全實現(xiàn), 例如, renderResponse.setTitle (), 同時你可以從Apache的CVS下載最新的Pluto代碼, 希望這編文章可以幫大家搭建一個簡單的Portlet開發(fā)環(huán)境 , 熟悉Portlet相關(guān)的知識.
資源:· Pluto
http://jakarta.apache.org/pluto
· Pluto Mail List
http://news.gmane.org/gmane.comp.jakarta.pluto.user
· WSRP Spec1.0
http://www.oasis-open.org/committees/tc_home.php?wg_abbrev=wsrp
· Apache的WSRP實現(xiàn)
http://ws.apache.org/wsrp4j/
· Apache’s Portal, JetSpeed:
http://jakarta.apache.org/jetspeed/site/index.html
· JSR 168:
http://www.jcp.org/en/jsr/detail?id=168
· "Portlet 規(guī)范介紹" By Stefan Hepper 和 Stephan Hesmer
o Part 1: Get your feet wet with the specification's underlying terms and concepts (August 2003)
o Part 2: The Portlet API's reference implementation reveals its secrets (September 2003)