By
Terry. Lee
本系列將介紹如何開發基于
Portlet Specification v1.0 (JSR168)
的
Portlet
應用程序以及相關的概念. 這里使用
Pluto v1.0
作為
Portal/Portlet Container
.
并且可以將開發完成的
Portlet
應用程序發布到任何遵循
JSR168
規范的其他
Portlet Container
和
Portal Server
上.
·??
為什么要發布
Portlet Specification
?
什么是
JSR168
?
越來越多的公司開發了各自的
Portal
組件和基于其的
Portal
產品(如
Bea, IBM, Oracle, Sun, Sybase, Vignette, Novell, SAP, Plumtree
, Apache
等.
這種互不兼容的接口實現帶給軟件開發商以及Web開發人員各種問題, 為了解決這些問題,
JCP
發布了
JSP168 (Java Specification Request), Portlet Specification v1.0,
用以提供不同
Portal
和
Portlets
的實現
之間的互通性.可能許多軟件開發商 (如上所列) 提供更為強大的
Portlet
實現, 但是如果希望開發人員希望所開發的
Portlet
程序
能夠不依賴于某一種或者幾種平臺, 那么使用
JSR168 Portlet
毫無疑問是你的首選.
·????????
什么是
Portal?
Portal
是基于
WEB
的應用程序, 一個信息平臺, 它將不同來源的各種資源進行整合并集中展現給客戶.
通常其有如下三個特點:
a.????????????????
P
ersonalization (
個性化
)
b.????????????????
Single sign on
(
單點登陸
)
c.?????????????????
Content aggregation (
內容聚合
)
Personalization
是
Portal
提供的特性之一
,
用來提供用戶的個性化設置
.
Single sign on
是
J2EE
的一個特性
.
其中
Content aggregation
是
Portal
比較有特色的特性
,
它將不同來源的信息整合到一個同一個頁面中
,
使得用戶可以更便捷
,
更快速的進行某些商業應用
.
這里舉一個簡單的商業應用的例子
,
如果某一客戶需要進行一次商業采購行為
,
以往需要訪問不同的產品供應商的主頁得到相關信息
,
這往往是一個耗時耗力的過程
,
但如果使用
Portal
將所有經常使用的相關商品供應商的商品
瀏
頁面都整合到一個
Portal
頁面中
,
那么所有的供應商的商品都可以更快的被瀏覽
,
篩選
,
加快了客戶的商業運作效率
.
·????????
什么是
Portlet?
Portlet
是一種基于
WEB
組件的
JAVA
技術, 由
Portlet Container
進行管理. 處理請求并動態返回頁面, 可以作為
Portal
的可
即插即用
的界面組件.
?
·????????
什么是
Portlet Container? Portlet Container
用來管理
Portlet
的生命周期并且提供其運行所需要的必要環境. 并且給
Portlet Preferences
提供持久性(
Persistent
)
存取服務.但是其不支持
Portlet
的
aggregation(
內容聚合
).
內容聚合
由
Portal
組件提供.這個概念需要弄清楚.
注:
Portlet Preferences
是
Portlet
的一個新特性,提供類似數據庫的功能.但是不是用來取代數據庫. 只能用來存取簡單的
Portlet
配置參數.
???????
·????????
什么是
WSRP?
WSRP
是
OASIS Web Service for Remote Portlets
的縮寫
. WSRP
是
Web service
的一種新的商業應用
,
一種新的標準
,
主要用來簡化
Portal
對于各種資源或者程序整合的復雜性
,
可以避免編程帶來的整合麻煩和問題
.
而且
Portal
的管理員可以從海量的
WSRP
服務中選擇需要的功能用以整和到目前使用的
Portal
中
.
它有三種
Roles:
1) Producer ->
提供
Portlet
2) Consumer ->
使用
Portlet
3) End User ->
最終用戶
它的特點在于
Producer
將
Consumer
所需要的信息通過
WSRP
返回給
Consumer,
這些信息是相對的標記
Fragment(
片段
),
例如
HTML,XHTML
等
,
直接可以鑲入用戶的
Page
中
,
而不用象
Web service
一樣需要單獨開發用戶端接口
.
再舉個
WSRP
的商業應用的例子:
如果一個
客戶需要采購一些
PC
軟件
,
那么這個客戶通過互聯網登陸某家
PC
軟件
代理供應商的網頁
,
查詢相關信息
,
但是做為
軟件
代理公司
,PC
軟件的高級使用指南及配置工具是非常缺乏或者是無法及時更新的
,
這樣一來使得用戶需要登陸軟件開發商的站點上才能得到相關信息
.
然后再回到代理商主頁進行訂購
.
而比較理想的商業應用應該是代理公司可以整合軟件開發商提供的使用指南或者配置工具
(
可以由開發商及時更新
).
但是如果使用
XML API,
那么需要針對不同軟件提供商開發不同的接口實現
.
而使用
WSRP
可以將相關的信息及工具直接鑲入到代理商的頁面用以動態
,
及時提供給客戶
.
WSRP4J
是
Apache
的
WSRP
標準實現
,
請參考文章末尾的資源部分.
·??
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
的一個
Open Source
項目
,
是基于
Portlet Spec (v1.0) (JSR168)
的一個
Portlet Container
的實現
.
它也提供了
一個演示版的
Sample
Portal
實現
,
用以進行
Portlet Container
的
測試
.
但是功能相對簡單
:
?? ?
例如
,
?? ?1)
沒有復雜的
Layout
實現
.
?? ?2)
不是
multi
-user enabled
,
比如
,
不同
User
之間的
Portlet Preferences
互相是
?
可以
share
的
.??
??
請記住
Pluto
只是一個
Portlet Container
的實現
,
不是一個
Portal
的實現
.
如果你需要功能更為強大的
Portal
,
可以使用
JetSpeed
或者其他功能更強大的
Portal
,
它同樣也是
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
·????????
開發工具
( Eclipse2.1, Pluto-plugin, Jakarta-tomcat-
下載地址
:
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
·??
配置開發環境
??
???
Step1 :?
解壓
eclipse-SDK-
?Step2 :?
解壓
org.eclipsefan.pluto.ui_1.0.0.zip ,
并將其拷入
eclipse\plugins
目錄
下
,
如下
: eclipse\plugins\org.eclipsefan.pluto.ui_
\Icon
\Lib
\Source
\Webapp
…
??????????
Step3 :?
解壓
tomat4.1
到一目錄
.
并且配置好
Tomcat
服務器
.
?????
?????????????
配置完成
.
a.
使用
Portlet Wizard
來創建
Portal
1)
選擇
File > New > Project… > Portal > Pluto Portal application,
單
?
?????
擊
Next
,
如圖
:
Figure 1.2
2)
指定
Project
名稱
(
如圖
1.3),
這里使用
pluto
,
然后單擊
Next
.
Figure 1.3
3)
指定
Tomcat
路徑
,
單擊
Finish
.?
之
后
eclipse
會將
Pluto
安裝到
tomcat
上
,
并且自動配置好環境
.
Figure 1.4
b.
創建
Portlets Application
1)
創建
Pluto portlet application
(
如圖
Figure 1.5
)
?????????????????????????????????????????
Figure 1.5
????????????? 2)
輸入
project
名稱
(
如圖
Figure 1.6
),
這里用
portlets
?????????????
???????????????????????????????????????????????????
Figure 1.6
?
??????
c.
最后生成的工程如圖
(
Figure 1.7
):
??
Figure 1.7
???????????????????
d.
通過
portlet.xml
生成
/
更新
web.xml
(
如圖
Figure 1.8)
?????
注
:
如果每次對
Portlet.xml
文件進行修改后
,
比如添加新的
Portlet ,
必須更新
?
web.xml ,
生成對應的
Wrap Servlet ,
否則
Portlet
無法在
Portal
中加載
.
??????
Figure 1.8
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
中看到它
,
雙擊就可以了
.
如上圖
(
Figure 1.8
):
其中
deploy.bat
文件就是實現
Deploy
的
.
4)
Start tomcat.
5)
在
IE
中輸入
:
http://localhost: 8080/pluto/portal
,
你應該能看到
portal
頁面.
·??
Sample Portlet
在
Portlets
工程文件創建過程中
,
自動生成許多文件
,
包括一個簡單的
Portlet
小程序
,
現在我們來簡單觀察一下生成的
sample
portlet.
1)
與
Servlet
非常類似
,
Portlet
擴展自
GenericPortlet
import
javax.portlet.*;
public
class
SimplePortlet
extends
GenericPortlet
2)
其三個方法對應了
Portlet
標題欄中的三個
鏈
接
(View, Edit, Help)
所執行的功能
.
public
void
doView(
…)
public
void
doEdit(
…)
public
void
doHelp(
…)
3)
三個方法分別調用了三個
JSP
文件
,
用以生成
Portlet fragment
,
同樣也可以調用
Servlet
產生
Portlet fragment
,
或者不調用
JSP
或者
Servlet,
直接在方法中得到
PrintWriter,
用最簡單的
pw.println(…)
打印出內容
,
類似
Servlet,
如下
:
PrintWriter pw=renderResponse.getWriter();
pw.println(
"
Hello, Portlet!
"
);
與
Servlet
應用類似
,
也可以使用
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文件,你會發現,和Servlet一樣,JSP中可以使用Portlet的一些
variables
.
例如:
renderResponse
renderRequest
portletConfig
但是必須聲名
:
<portlet:defineObjects/>
其中不可以定義任何屬性或者包含任何內容
.
2)
actionURL Tag
屬性
|
值類型
|
對應值
|
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>
創建一個
action URL ,
當訪問它時將使
portlet window
變為
normal
裝態
,
模式變為
edit.
3)
renderURL Tag
屬性
|
值類型
|
對應值
|
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=”
<portlet:param name=”page” value=”
</portlet:renderURL>
創建一個
render URL ,
當訪問它時將使
portlet window
變為
maximized
裝態
,
模式變為
view.
?
4)
namespace Tag
為目前的
Portlet
產生一個唯一的
Value.
防止和其他的
Portlet
或者
Portal
頁面上的
value
產生沖突
.
<A HREF=”javascript: <portlet:namespace/>doFoo()”>Foo</A>
5)
param Tag
屬性
|
值類型
|
name |
String |
<portlet:param name=”myParam” value=”someValue”/>
注
: param Tag
不可以有
body content.
總結
Pluto
目前是
v1.0
,
有部分
Portlet
規范
沒有完全實現
,
例如
,
renderResponse.setTitle (),
同時
你可以從
Apache
的
CVS
下載最新的
Pluto
代碼
,
希望這編文章可以幫大家搭建一個簡單的
Portlet
開發環境
,
熟悉
Portlet
相關的知識
.
資源
:
·??
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
實現
(WSRP4J)
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
規范介紹
" By Stefan Hepper
和
Stephan Hesmer