这段时间比较懒,不想写文章,结果blog也闲置快半年了

本教程介绍Rational Application Developer 7中JSR168 Portlet的开发,以两个例子说明JSR168 Portlet开发的基本内容和步骤,所实现Portlet的功能与文章《在Rational Application Developer中开发简单Portlet》一致。
IBM要放弃自己那套Portlet开发API的意思已经很明显了,在RAD7中这些API已经被打上波浪线,标识成“不建议使用类型”,deprecated了。RSA7中更直接,直接在类名上划黑线,明确告诉你最好不要在用了,呵呵

开发环境:WinXP+SP2,RAD 7.0.0.3
测试环境:Win2003+SP2,WebSphere Portal6
示例1:在Portlet编辑页面中添加用户登录界面,在视图页面中显示用户名和密码。
1. 启动RAD。
2. 新建Portlet项目。
选择“文件-》新建-》Portlet项目”。
将项目命名为PortletExample,选择“创建portlet”,RAD就会自动在项目中添加一portlet。由于是要部署在Portal6上的,目标运行时设置为“WebSphere Portal V6.0 stub”。Portlet API选择“JSR 168 Portlet”,我们要开发的就是它嘛,如果还是“IBM Portlet”,搞不好哪天就被IBM自己抛弃了
Portlet类型选择“基本Portlet”,RAD就会创建一示例portlet,我们只需在其上做一些修改即可。其它都接受默认即可。

将所有的视图都勾上。
取消“将操作侦听器添加至portlet以处理操作请求”,我们暂时不会涉及到该方面内容。然后“完成”即可。
项目建立完成后我们就可以看见如图所示的项目文件列表。
WebContent下包含了项目的jsp文件,五个jsp文件分别对应五种视图。
3.编写代码。
(1)在PortletExamplePortlet.java中定义三变量EDIT_USERNAME、EDIT_PASSWORD和EDIT_CANCEL。EDIT_CANCEL用于处理按钮的取消事件。
public
static
final
String
EDIT_USERNAME
=
"PortletExamplePortletEditUsername"
;
public
static
final
String
EDIT_PASSWORD
=
"PortletExamplePortletEditPassword"
;
public
static
final
String
EDIT_CANCEL
=
"PortletExamplePortletEditCancel"
;
(2)编辑PortletExamplePortlet.java,处理EDIT_SUBMIT和EDIT_CANCEL按钮事件,在函数processAction中针对编辑视图的提交请求和取消进行处理,将username和password两值存入到PortletPreferences中。
if
( request.getParameter(
EDIT_SUBMIT
) !=
null
) {
PortletPreferences prefs = request.getPreferences();
try
{
prefs.setValue(
EDIT_USERNAME
,request.getParameter(
EDIT_USERNAME
));
prefs.setValue(
EDIT_PASSWORD
,request.getParameter(
EDIT_PASSWORD
));
prefs.store();
}
catch( ReadOnlyException roe ) {
}
catch( ValidatorException ve ) {
}
response.setPortletMode(PortletMode.
VIEW
);
}
if
( request.getParameter(
EDIT_CANCEL
) !=
null
) {
response.setPortletMode(PortletMode.
VIEW
);
}
使用response.setPortletMode(PortletMode.VIEW);语句决定提交后将转向的视图。
(3)编辑PortletExamplePortletEdit.jsp,删除RAD自动生成的代码,设计用户名和密码提交页面。
<%@
page
session
=
"false"
contentType
=
"text/html"
pageEncoding
=
"GB18030"
import
=
"javax.portlet.*,portletexample.*"
%>
<%@
taglib
uri
=
"http://java.sun.com/portlet"
prefix
=
"portlet"
%>
<
portlet:defineObjects
/>
<%
PortletPreferences prefs = renderRequest.getPreferences();
if
(prefs !=
null
) {
String username = prefs.getValue(PortletExamplePortlet.EDIT_USERNAME, ""
);
String password = prefs.getValue(PortletExamplePortlet.EDIT_PASSWORD, ""
);
%>
<
FORM
ACTION
=
"<portlet:actionURL/>"METHOD="POST">
Username:
<
INPUT
NAME
=
"<%=PortletExamplePortlet.EDIT_USERNAME%>"VALUE="<%=username%>"TYPE="text"><BR>
Password:
<
INPUT
NAME
=
"<%=PortletExamplePortlet.EDIT_PASSWORD%>"VALUE="<%=password%>"TYPE="password"><BR>
<
INPUT
NAME
=
"<%=PortletExamplePortlet.EDIT_SUBMIT%>"TYPE="submit" value="Save">
<
INPUT
NAME
=
"<%=PortletExamplePortlet.EDIT_CANCEL%>"TYPE="submit" value="Cancel">
</
FORM
>
<%
}
else
{
%>
Error: PortletPreferences is null.
<%
}
%>
注意标签<portlet:defineObjects/>,在页面中添加它后才可调用PortletConfig、RenderRequest、RenderResponse这几个对象。
(4)编辑PortletExamplePortletView.jsp,在页面上输出用户名和密码值。
<%@
page
session
=
"false"
contentType
=
"text/html"
pageEncoding
=
"GB18030"
import
=
"javax.portlet.*,portletexample.*"
%>
<%@
taglib
uri
=
"http://java.sun.com/portlet"
prefix
=
"portlet"
%>
<
portlet:defineObjects
/>
<%
PortletPreferences prefs = renderRequest.getPreferences();
if
(prefs !=
null
) {
String username = prefs.getValue(PortletExamplePortlet.EDIT_USERNAME, ""
);
String password = prefs.getValue(PortletExamplePortlet.EDIT_PASSWORD, ""
);
%>
<
P
>
<%=
PortletExamplePortlet.EDIT_USERNAME
%>
:
<%=
username
%>
</
P
>
<
P
>
<%=
PortletExamplePortlet.EDIT_PASSWORD
%>
:
<%=
password
%>
</
P
>
<%
}
else
{
%>
Error: PortletPreferences is null.
<%
}
%>
4.全部修改完成后保存,导出WAR安装包。
指定导出位置。
5.在Portal6上安装,新建一测试页面将该Portlet添加到页面上。
初始运行界面如图,由于最初用户名和密码都为空,所以显示的也都是空的。
点击右上角小三角打开portlet菜单,点击个性化,即进入编辑模式,输入用户名密码然后保存。
此时页面转往视图模式,显示刚才设置的用户名和密码值。
如在个性化设置中点击取消,则所做改动无效,直接转回视图模式。
至此,第一个Portlet示例就开发完成了。
读者可以试着实现在配置模式中完成相同功能。