By Terry.lee
SpiritSeekerS@sqatester.com
?
?
本部分使用本系列中
Part1
搭建的開發(fā)環(huán)境
,
如果還沒有搭建好開發(fā)環(huán)境
,
請參考
Portlet
應(yīng)用開發(fā)
(JSR168),Part1
進行開發(fā)環(huán)境的搭建
.
?
在
Part1
中
,
我們開發(fā)了一個簡單的
portlet
程序
,
但是
幾乎
所有的
代碼和文件
都是由
Eclipse
的
pluto plugin
替我們完成的
,
如何配置用來在
Pluto
中的
Portal
中添加新的
Portal Page
并且將新的
Portlet
添加到新的
Page
中去呢
? Part2
將一步一步教你如何創(chuàng)建一個新
Portal Page.
?
?
·
????????
Portal
的設(shè)定
Tomcat
中
Webapps
目錄
:
其中
pluto
是
portal
所在目錄
,
用以配置
Portal Page
的兩個文件分別是
pageregistry.xml
和
portletentityregistry.xml (
如圖
Figure1_1)
Figure1_1
?
?
其中
pageregistry.xml
用來在
Portal
中配置
Portal Page,
而
portletentityregistry.xml
用來在
Page
中配置
Portlet.
?
·
????????
portletentityregistry.xml
打開
pageregistry.xml,
內(nèi)容如下
:
<?xml version="1.0" encoding="UTF-8"?>
<portlet-entity-registry>
??? <application id="1">
??????? <definition-id>portlets</definition-id>
??????? <portlet id="1">
??????????? <definition-id>portlets.SimplePortlet</definition-id>
??????? </portlet>
??? </application>
</portlet-entity-registry>
其中
application
標(biāo)簽表明了目前的
application
的
ID, portlet
標(biāo)簽定義了一個
Portlet ,
如果你新開發(fā)了一個
Portlet ,
可以加入以下內(nèi)容
:
<?xml version="1.0" encoding="UTF-8"?>
<portlet-entity-registry>
??? <application id="1">
??????? <definition-id>portlets</definition-id>
??????? <portlet id="1">
??????????? <definition-id>portlets.SimplePortlet</definition-id>
??????? </portlet>
??????? <portlet id="2">
??????????? <definition-id>portlets.IntroPortlet</definition-id>
??????? </portlet>
??? </application>
</portlet-entity-registry>
?
這樣一來你又在
portal
中注冊了一個新的
Portlet,
接下來是將新注冊的
Portlet
加入
Page
中
.
?
·
????????
pageregistry.xml
打開
pageregistry.xml,
內(nèi)容如下
:
<?xml version="1.0"?>
<portal>
?
??? <fragment name="navigation" class="org.apache.pluto.portalImpl.aggregation.navigation.TabNavigation">
??? </fragment>
???
??? <fragment name="sample" type="page">
??????? <navigation>
??????????? <title>Sample Portlet</title>
??????????? <description>Basic page to show the simple portlet</description>
??????? </navigation>
?
??????? <fragment name="row" type="row">
?
??????????? <fragment name="col1" type="column">
?
????????
???????<fragment name="p1" type="portlet">
??????????????????? <property name="portlet" value="1.1"/>
??????????????? </fragment>
?
??????????? </fragment>
?
??????? </fragment>
?
??? </fragment>
</portal>
?
以上是
sample portlet
的配置
.
?
Fragment
標(biāo)簽用來配置
navigation
(
導(dǎo)航欄
),
Page,
以及
Page
中的
Column
和
Row.
分別用
fragment
標(biāo)簽的
type
屬性來標(biāo)識
. Pluto
自帶的
Portal
使用
fragment
標(biāo)簽來配置及處理簡單的
Portlet Layout.
?
<property name="portlet" value="1.1"/>
中的
value
值是在
portletentityregistry.xml
中定義的
.
分別對應(yīng)
application id
和
portlet id
,
將其值用
”.”
連接起來使用用以定義一個
Portlet
.
同樣
,
如果使用
<property name="portlet" value="1.2"/>
,
則將使用
portlets.IntroPortlet.
????????
?
?
1)????
配置同一行中的兩個
Portlet,
如下
:
<?xml version="1.0"?>
<portal>
?
??? <fragment name="navigation" class="org.apache.pluto.portalImpl.aggregation.navigation.TabNavigation">
??? </fragment>
???
??? <fragment name="sample" type="page">
??????? <navigation>
??????????? <title>Sample Portlet</title>
??????????? <description>Basic page to show the simple portlet</description>
??????? </navigation>
?
??????? <fragment name="row" type="row">
?
??????????? <fragment name="col1" type="column">
?
??????????????? <fragment name="p1" type="portlet">
??????????????????? <property name="portlet" value="1.1"/>
??????????????? </fragment>
?
??????????????? <fragment name="p2" type="portlet">
??????????????????? <property name="portlet" value="1.1"/>
??????????????? </fragment>
?
??????????? </fragment>
?
??????? </fragment>
?
??? </fragment>
?
</portal>
?
2)
????
配置同一列中的
兩個
Portlet,
如下
:
<?xml version="1.0"?>
<portal>
?
??? <fragment name="navigation" class="org.apache.pluto.portalImpl.aggregation.navigation.TabNavigation">
??? </fragment>
???
??? <fragment name="sample" type="page">
??????? <navigation>
??????????? <title>Sample Portlet</title>
??????????? <description>Basic page to show the simple portlet</description>
??????? </navigation>
?
??????? <fragment name="row1" type="row">
?
??????????? <fragment name="col1" type="column">
?
??????????????? <fragment name="p1" type="portlet">
??????????????????? <property name="portlet" value="1.1"/>
??????????????? </fragment>
?
??????????? </fragment>
?
??????? </fragment>
?
??????? <fragment name="row2" type="row">
?
??????????? <fragment name="col1" type="column">
?
??????????????? <fragment name="p1" type="portlet">
??????????????????? <property name="portlet" value="1.1"/>
??????????????? </fragment>
?
??????????? </fragment>
?
??????? </fragment>
?
??? </fragment>
?
</portal>
?
?
?
?
·
????????
Portlet Modes
和
Portlet window state
s
Portlet
模式
(Portlet Mode)
是
Portlet
提供的用以區(qū)分
Portlet
所執(zhí)行功能的一個概念
.
通常情況下其擁有以下幾種模式
:
1. VIEW
2. EDIT
3. HELP
以上各模式分別對應(yīng)
GenericPortlet
中的
doView(…) , doEdit(…) , doHelp(…)
方法
,
分別調(diào)用以上方法來產(chǎn)生各個模式中的
Fragment
內(nèi)容
.
非常類似
Servlet
中的
doGet(…) , doPost(…)
方法
,
都是
Helper
方法
,
但是概念不同
.
?
Portlet
狀態(tài)
(Portlet window states)
提供了對于
Portlet
窗口的控制功能
,
其中有如下三種最基本的狀態(tài)
:
1.
????
Normal
2.
????
Maximized
3.
????
Minimized
?
Portlet
開發(fā)人員可以在處理
ActionRequest
(
以后的章節(jié)將講述其概念
)
時使用代碼實現(xiàn)
Portlet
模式
,
及其
Portlet
狀態(tài)的轉(zhuǎn)變
.
?
注
:
只能在處理
ActionRequest
時改變
Portlet Modes
和
Portlet Window states.
?????
以上
Portlet Modes
和
Portlet window states
都可以配置成
custom Portlet mode
和
custom portlet window state.
不同的地方在于對于
Mode
來說
,
定制化的
Mode
需要對
GenericPortlet
的
doDispatch
方法進行重寫
(Overriding),
因為
GenericPortlet
類通過
render
方法按照不同的
Portlet Mode
將
request
分別分發(fā)給
doView,doEdit,doHelp
等輔助方法
.
如果需要定制的
Mode ,
必須重寫
doDispatch
方法
.
同時如果使用
Portal
提供商的
Portlet Modes
或者
Portlet window States,
都必須在部署描述中添加相關(guān)的設(shè)定
.
?
A.
????
添加
Custom Portlet Modes
以下使用代碼實現(xiàn)了一個新的
Portlet Mode: CONFIG
,
當(dāng)然也可以使用
Portal
提供商支持的
Portlet Mode.
但是需要
Mapping
到
Portal
提供商所支持的
Portlet Mode.
使用如下描述
:
?
??? <custom-portlet-mode>
???????? <description>Provides administration functions</description>
???????? <portlet-mode>CONFIG</portlet-mode>
??? </custom-portlet-mode>
?
1. Portlet
代碼
(
CustomPortletModeExample
.java)
?
package
portlets.portletmode;
?
/**
?
*
@author
terry
?
*
?
*
To
change
the
template
for
this
generated
type
comment
go
to
?
*
Window>Preferences>Java>Code
Generation>Code
and
Comments
?
*/
import
javax.portlet.*;
import
java.io.IOException;
?
public
class
CustomPortletModeExample
extends
GenericPortlet{
?
?
public
void
doView(RenderRequest
request,
RenderResponse
response)
?
throws
PortletException,
IOException
?
{
?
response.setContentType(
"text/html"
);
?
?
String
jspName
=
getPortletConfig().getInitParameter(
"all"
);
?
?
PortletRequestDispatcher
rd
=
?
?
getPortletContext().getRequestDispatcher(jspName);
?
?
rd.include(request,
response);
?
}
?
?
public
void
doEdit(RenderRequest
request,
RenderResponse
response)
?
throws
PortletException,
IOException
?
{
?
response.setContentType(
"text/html"
);
?
?
String
jspName
=
getPortletConfig().getInitParameter(
"all"
);
?
?
PortletRequestDispatcher
rd
=
?
?
getPortletContext().getRequestDispatcher(jspName);
?
?
rd.include(request,
response);
?
}
?
?
?
public
void
render(RenderRequest
request,
RenderResponse
response)
?
throws
PortletException,
IOException{
????????
doDispatcher(request,response);
?
}
?
?
?
public
void
doDispatcher(RenderRequest
request,
RenderResponse
response)
????????
throws
PortletException,
IOException{
???????? if(!request.getWindowState().equals(WindowState.MINIMIZED)){
????????
if
(request.getPortletMode().equals(PortletMode.VIEW)){
???????????????
doView(request,response);
????????
}
else
if
(request.getPortletMode().equals(PortletMode.EDIT)){
???????????????
doEdit(request,response);
????????
}
else
if
(request.getPortletMode().equals(
new
PortletMode(
"CONFIG"
))){
???????????????
doConfig(request,response);
????????
}
}
?
}
?
?
public
void
doConfig(RenderRequest
request,
RenderResponse
response)
?
?
throws
PortletException,
IOException
?
{
?
?
response.setContentType(
"text/html"
);
?
?
?
String
jspName
=
getPortletConfig().getInitParameter(
"all"
);
?
?
?
PortletRequestDispatcher
rd
=
????????
getPortletContext().getRequestDispatcher(jspName);
?
?
?
rd.include(request,
response);
?
}
?
?
public
void
processAction(ActionRequest
request,
ActionResponse
response)
????????
throws
PortletException,
IOException
?
{
????????
String
action=request.getParameter(
"ACTION"
);
????????
System.out.println(
"ACTION"
+
action);
????????
if
(action==
null
){
???????????????
action=
""
;
????????
}
????????
response.setRenderParameter(
"ACTION"
,action);
?
}
}
?
2. JSP(all_mode.jsp)
<%@ page session="false" %>
<%@ page import="javax.portlet.*"%>
<%@ page import="java.util.*"%>
<%@ taglib uri='/WEB-INF/tld/portlet.tld' prefix='portlet'%>
<portlet:defineObjects/>
<br>
<h3>Custom Portlet Mode Example</h3>
Current Portlet Mode: <b><%=renderRequest.getPortletMode()%></b><br>
Current
Window
State
: <b><%=renderRequest.getWindowState()%></b><br>
<br>
?
?
5. Portlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<portlet-app xmlns="/org/apache/pluto/portalImpl/xml/portlet-app_1_0.xsd" version="1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="/org/apache/pluto/portalImpl/xml/portlet-app_1_0.xsd /org/apache/pluto/portalImpl/xml/portlet-app_1_0.xsd">
?
? <!-- Custom PortletMode Example -->
? <portlet>
???????? <description>CustomPortletModeExample Example</description>
???????? <portlet-name>CustomPortletModeExample</portlet-name>
???????? <display-name>CustomPortletModeExample</display-name>
???????? <portlet-class>portlets.portletmode.CustomPortletModeExample</portlet-class>
???????? <init-param>
??????????????? <name>all</name>
??????????????? <value>/fragments/portletmode/all_mode.jsp</value>
???????? </init-param>
???????? <expiration-cache>-1</expiration-cache>
???????? <supports>
??????????????? <mime-type>text/html</mime-type>
??????????????? <portlet-mode>VIEW</portlet-mode>
??????????????? <portlet-mode>EDIT</portlet-mode>
??????????????? <portlet-mode>CONFIG</portlet-mode>
???????? </supports>
???????? <supported-locale>en</supported-locale>
???????? <portlet-info>
?
????????????? <title>Custom PortletMode Example</title>
??????????????? <short-title>PortletMode</short-title>
??????????????? <keywords>PortletMode</keywords>
???????? </portlet-info>
??? </portlet>
?
</portlet-app>
6. portletregistry.xml.
?
<?xml version="1.0" encoding="UTF-8"?>
<portlet-entity-registry>
??? <application id="10">
??????? <definition-id>portlets</definition-id>
??????? <portlet id="10">
??????????? <definition-id>
portlets.CustomPortletModeExample
</definition-id>
??????? </portlet>
??? </application>
</portlet-entity-registry>
?
7. pageregistry.xml
<?xml version="1.0"?>
<portal>
?
??? <fragment name="navigation" class="org.apache.pluto.portalImpl.aggregation.navigation.TabNavigation">
??? </fragment>
???
??????
? <!-- Custom PortletMode Example Page -->
???
??<fragment name="customportletmodepage" type="page">
??????? <navigation>
??????????? <title>Custom PortletMode Example Page</title>
??????????? <description>Custom PortletMode Example Page</description>
??????? </navigation>
??????? <fragment name="row1" type="row">
??????????? <fragment name="col1" type="column">
??????????????? <fragment name="p1" type="portlet">
??????????????????? <property name="portlet" value="10.10"/>
??????????????? </fragment>
??????????? </fragment>
??????? </fragment>
? </fragment>
?
</portal>
?
?
將以上源代碼編譯后
,
再通過
Eclipse
生成
/
更新
Portlet
的
web.xml
后
,?
將所有配置及相關(guān)文件部署后
,
啟動
Tomcat.
?
?
?
在
Browser
中加載如下頁面
:
Http://localhost:8080/pluto/portal ,
可以看到如下的頁面
(
圖
:2_1)
?
?????
????????????????????????
圖
2-1
?
?
單擊
Custom PortletMode Example Page
后可以看到如下
Portlet
頁面
:
?
圖
2-2
?
單擊右上角的
config
后
,
可以看到如下頁面片段
:
?
圖
2-3
?
以上可以看到新添加的
Portlet Mode: config.
?
?
B.
????
添加
Custom Window States
?
?
添加新的
window states
同樣需要在配置文件中加入相應(yīng)的描述
,
如下
:
<custom-window-state>
<description>Occupies 50% of the portal page</description>
<name>half_page</name>
</custom-window-state>
?
但是同樣需要
Mapping
到
Portal
提供商所支持的
Window state.
?
總結(jié)
:
因為
Eclipse
的
Pluto plugin
使用的是
version1.0 ,
所以可能有許多
Bugs
或者沒有實現(xiàn)的功能
,
所以如果需要最新版本的
Pluto ,
可以在
Apache
的
CVS
上下載
,
下載的代碼中有現(xiàn)成的
Build scripts
以及相關(guān)的
deploy script.
請參考相關(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
Part 1: Get your feet wet with the specification's underlying terms and concepts (August 2003)
Part 2: The Portlet API's reference implementation reveals its secrets
(September 2003)