IBM 開發人員解決方案:開發使用了 JavaBeans 和 JSP 組件的 portlet
2024-09-05 00:19:27
供稿:網友
ibm 開發人員解決方案:開發使用了 javabeans 和 jsp 組件的 portlet
使用 portal toolkit plugin for websphere studio application developer v4.0.3 輕松實現它。
balu ramachandran ([email protected])
高級軟件工程師,ibm
2003 年 2 月
本文敘述了 websphere studio application developer v4.0.3 能成為開發 portlet 的首選 j2ee 開發工具的緣由。文章簡要地討論了門戶網站、websphere portal server v4.1.2、portlet 和 portal toolkit plugin v4.x。作者將為您詳細講解如何使用 portal toolkit plugin 一步一步地編寫 portlet。文中的示例門戶網站應用程序就是一個 portlet,這個 portlet 能調用一個 javabeans 組件把存儲在 java 屬性文件中的特定于的客戶信息聚集在一起。這個 portlet 將使用 javabeans 信息并通過 javaserver pages(jsp)頁面將結果返回給請求者。當需要向用戶顯示時,這個 jsp 頁面將被嵌入到 portlet 中。
引言
門戶網站是一個通過把基于 web 的資源聚集一處使用戶可以單點訪問那些資源的 web 站點。用戶不再需要單獨登錄每個他們想要使用的應用程序而只需要登錄門戶網站本身就可以了。
websphere portal server v4.1
ibm websphere portal 是 websphere 軟件平臺的一個組成部分, 它主要完成下述三種功能:
通過一系列用戶、設備和定制選項提供對信息的訪問。
集成并自動執行業務過程。
構建、連接并管理應用程序。
websphere portal server 是一個高度可伸縮門戶網站服務器,它實現了一個戰略上的開放源代碼門戶網站框架。portal server v4.1 建立在 websphere portal family v2.1 的核心服務之上。portal server 提供以下服務:
對各類門戶網站和商業服務進行訪問,比如:訪問用于電子交易(ecommerce)的 websphere commerce suite、訪問用于信息分類(information categorization)的 eip 和訪問用于高級協作(advanced collaboration)與個人信息管理(personal information management (pim))的 lotus 工具(例如:calendar、mail 和 sametime)。
通過工作流、eai 等方法對舊系統的連接。
websphere portal 也提供下列其它服務:
單點登錄
安全性
web 內容發布
搜索
個性化處理
協作服務
企業應用程序集成
支持移動設備
站點分析
portlet
portlet 是 ibm websphere portal 的重要組成部分。作為一種特殊的可重用的 java servlet,portlet 被作為已定義區域顯示在門戶網站頁面上。portlet 可以訪問許多不同的應用程序、服務和 web 內容。websphere portal 配有一個豐富的標準 portlet 集,它包括了用于顯示辛迪加內容、用于轉換 xml 和用于訪問搜索引擎與 web 頁面的 portlet。您也可以使用一些第三方提供的 portlet ,例如企業資源計劃(erp)和客戶關系管理(crm)。 portal server 還提供您一個能用于創建自定義 portlet 的 api。通過把 portlet 聚集在一起并授予用戶定義他們自己的桌面的權力,portal server 為商業伙伴、客戶或者雇員提供了一條能更高效更滿意地從事商務活動的捷徑。
websphere studio application developer v4.0.3
不久之后,websphere studio 產品套件會取代現有的 java 和 web 應用程序開發工具(visualage for java 和 websphere studio-classic)。新的工具建立在一個開放的平臺之上并且擁有最前沿的 enterprise javabeans(ejb)技術和基于 j2ee 1.2 規范的 j2ee 加工技術。它還具有下列功能:
企業連接
內置單元測試環境(websphere application server 和 tomcat)
增量編譯
多種調試方法(包括遠程調試)
集成小組開發環境
websphere studio development environment 是建立在高度可插的 websphere studio workbench 開放源代碼平臺之上。在這個平臺的頂層,ibm 實現了 websphere studio application developer, 并且 ibm 還鼓勵其他供應商將他們的工具也集成到這個平臺上。這個開發環境適合基于角色的開發,并且開發過程帶有適合于特定角色的用戶界面功能。所有資源都存儲在一個基于文件的資源庫中。在不久的將來,visualage for java 和 websphere studio-classic 所有的功能將都能在 application developer 中實現。新的工具已經能支持 web 服務。
portal toolkit plugin
portal toolkit 是 application developer v4.0.3 的一個插件,用于開發 portlet 應用程序。portal toolkit 提供:
portlet 項目,您能在這個項目中創建抽象 portlet、 jsp portlet、servlet invoker portlet、 xml/xsl portlet 和多 - 設備/視圖(multi-device/view)portlet。
portal server 項目,您能在這個項目中將你的 portlet 應用程序發布到你的目標 portal server 機器上。你的 portlet 將出現在你的 portal server 的調試頁面上。
用于企業應用程序的 portlet 應用程序示例。
開發并部署一個 jsp 會話 portlet
作為一個示例,我將為您講解如何開發一個使用了名為 portalbean 的 javabeans 組件的 portlet,使它能從屬性文件中檢索已存儲的值。這個 portlet 實例化一個已存儲的 javabeans 類并把這個 javabeans 實例存儲在 portlet 會話的請求屬性中。這之后,portlet 就調用一個 jsp 組件并傳出請求屬性與響應屬性的值。然后該 jsp 組件將會使用被傳來的 javabeans 類并在 portlet 中輸出適當的值。
為 application developer 安裝 portal toolkit plugin
如果您還沒有 portal toolkit plugin,請在這里下載 portal toolkit plugin。
根據提示將插件安裝到 application developer v4.0.3 中。請注意安裝的先決條件。
將 websphere application server jvm 添加到 aplication developer 的可用 jvm 列表中:
打開 windows - preferences。選擇 java - installed jre。
單擊 add new。
瀏覽并選擇 x:/websphere/appserver/java。
一旦它被顯示在已安裝 jre 列表中, 選中 was jre 旁的復選框將它設置為缺省的 jre。
單擊 ok 保存您所做的更改。
開發 myjspportal portlet
創建一個名為 jspportal 的新項目:
單擊 open new project wizard>portlet development>portlet application project。
在 project name 欄中輸入 myjspportal。
接受企業應用程序項目名稱(enterprse application project name)的缺省值
單擊 next.
安裝一個基本的 portlet 基礎架構,選擇 portlet type none。單擊 finish。
創建一個名為 myjspportal 的 java 類:
單擊 open new project wizard>java>java class。
將包命名為 portal。
將類命名為 myjspportal。
單擊 finish。
為 myjspportal 類復制并粘貼代碼:
請確認用于編輯 myjspportal 的編輯器是打開的。
復制并粘貼下面的代碼:
myjspportal
package portal;
import java.io.*;
import java.util.*;
import org.apache.jetspeed.portlet.*;
public class myjspportal extends abstractportlet {
public myjspportal() {
super();
}
public void doview(portletrequest request, portletresponse
response) throws ioexception,portletexception
{
// read the values from properties text file
getpropertiesfile(request);
printwriter writer = response.getwriter();
fileinputstream propfile = null;
// use created session to retrieve values
portletsession session = request.getportletsession(false);
// when valid session is returned, get the values from
the session
string lname = "";
string fname = "";
string email = "";
if (session!=null)
{
lname = (string)session.getattribute("lname");
fname = (string)session.getattribute("fname");
email = (string)session.getattribute("email");
}
portalbean bean = new portalbean();
bean.setlname(lname);
bean.setfname(fname);
bean.setemail(email);
request.setattribute("mybean",bean);
getconfig().getcontext().include("myportal.jsp",request,
response);
}
public void init(portletconfig portletconfig)
throws unavailableexception
{
super.init(portletconfig);
}
public void getpropertiesfile(portletrequest
request) throws portletexception
{
// intialize method variables
string fname = "";
string lname = "";
string email = "";
fileinputstream fi = null;
// create a new session
portletsession session =
request.getportletsession();
// initialize a properties file with no default
properties
properties myprop = new properties();
// load the properties class with key-value
pairs from // the text file
try{
fi = new fileinputstream
("c://temp//myproperties.txt");
myprop.load(fi);
fi.close();
}catch (filenotfoundexception fe)
{
system.out.println("file not found");
}
catch (ioexception fe)
{
system.out.println("io exception");
}
// retreive the individual key - value pairs and
store them in the session
if (myprop.containskey("firstname"))
{
fname = myprop.getproperty("firstname");
session.setattribute("fname", fname);
}
if (myprop.containskey("lastname"))
{
lname = myprop.getproperty("lastname");
session.setattribute("lname", lname);
}
if (myprop.containskey("email"))
{
email = myprop.getproperty("email");
session.setattribute("email", email);
}
}
public void service(portletrequest portletrequest,
portletresponse portletresponse) throws portletexception,
ioexception
{
doview(portletrequest,portletresponse);
}
}
編譯上述代碼(ctrl-s)。
創建如下所示的一個名為 myproperties.txt 的文本文件,并將它保存在 c:/temp 目錄中。
myproperties.txt
firstname=joe
lastname=generic
email=com
創建另一個名為 portalbean 的 java 類:
就象您先前所做的那樣,創建另一個名為 portalbean 的新 java 類,并將它打開。
將下面的代碼剪切并粘貼到 portalbean 中:
portalbean
package portal;
/**
* insert the type's description here.
* creation date: (1/12/2002 6:34:35 pm)
* @author: administrator
*/
public class portalbean {
/**
* portalbean constructor comment.
*/
public string fname;
public string lname;
public string email;
public portalbean() {
super();
}
/**
* insert the method's description here.
* creation date: (1/14/2002 10:28:20 pm)
* @return java.lang.string
*/
public string email() {
return null;
}
/**
* insert the method's description here.
* creation date: (1/14/2002 10:28:20 pm)
* @return java.lang.string
*/
public string getemail() {
return email;
}
public string getfname()
{
return fname;
}
/**
* insert the method's description here.
* creation date: (1/14/2002 10:35:04 pm)
* @return java.lang.string
*/
public string getlname() {
return lname;
}
/**
* insert the method's description here.
* creation date: (1/14/2002 10:29:41 pm)
* @param email java.lang.string
*/
public void setemail(string email) {
email = email;
}
public void setfname(string myname)
{
fname = myname;
}
public void setlname(string myname)
{
lname = myname;
}
}
在控制臺屏幕的下半部分會出現兩條錯誤信息 — 其中一條信息表明您需要將一個 servlet 引用添加到 portlet.xml 文件中而另一個信息則表明存在對 portlet.xml 文件中 portlet 的一個錯誤引用。 按下列步驟進行操作您將能修正這些錯誤。
將 portlet 添加到 web 部署描述符中:
雙擊 web.xml,打開 web.xml 進行編輯。
單擊 servlet 選項卡并選擇 add。
在 add servlet 中,找到 myjspportal servlet 并單擊 ok。
單擊 servlet 窗口并單擊 add 進行 url 映射。
編輯新的 url 并將它的值設置為: /myjspportal/*。
單擊 ctrl-s 保存您所做的更改。
單擊 web.xml 的 source 選項卡查看添加了 servlet 后相應的更改。
添加 myjspportal.jsp 文件:
右單擊 webapplication 并添加 new>jsp file。
輸入文件名 myportal.jsp。
單擊 finish。
雙擊新近創建的 myportal.jsp,將下面的代碼復制到文件中,然后單擊 ctrl-s 保存這些代碼。
myportal.jsp
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
< %@ page session="false" %;>
<jsp:usebean id="mybean" class="portal.portalbean" scope="request"/>
<meta http-equiv="content-type" content="text/html">
<meta http-equiv="content-style-type" content="text/css">
<table class="portlet" width="100%" border="0"cellspacing="0"
cellpadding="0">
<tr>
<td>
<b>last name: <%=mybean.getlname()%></b>
</td>
<td>
<b>first name: <%=mybean.getfname()%></b>
</td>
<td>
<b>email: <%=mybean.getemail()%></b>
</td>
</tr>
</table>
將參考 servlet 添加到 portlet.xml 文件中:
雙擊 portlet.xml, 打開 portlet.xml 進行編輯。
單擊 portlet 選項卡。
雙擊 portlet applications 并右單擊 portlet_2。
單擊 servlet 旁的 browse 并找到 myjspportal servlet。它的狀態應該是未被使用。
突出顯示 servlet 并單擊 ok。
使用 ctrl-s 保存并提交您剛才所做的更改。
當您完成上述步驟后,您就不會在控制臺屏幕上看到錯誤信息了。
重新編譯 myjspportal 項目:
右單擊 myjspportal 并選擇 rebuild project。
導出完整的 war 文件:
右單擊 myjspportal 并選擇 export war。
對于導出文件的存放位置,瀏覽并選擇 c:/temp(或其他任何合適的位置)作為導出文件位置,并輸入 myjspportal 作為文件名。
單擊 finish。
編譯代碼(ctrl-s)。
部署 myjspportal portlet
要部署 myjspportal portlet,請按下列步驟進行操作:
以門戶網站管理員身份登錄門戶網站:
打開 internet explorer 并輸入以下地址:http://portal.ibm.com/wps/portal(或合適的 url)。
單擊右上角的 key 圖標登錄。
使用 wpsadmin/wpsadmin 作為用戶標識/密碼,從而以管理員身份登錄。
訪問 administration portlet:
單擊左上角的下拉菜單,它包含一個表示當前所顯示 home 頁的條目,并選擇 portal administration。
單擊 browse 找到您在前面一節中導出的 myjspportal.war 文件(c:/temp/myjspportal.war),然后單擊 next。
單擊 install 安裝 portlet。您應該看見一條表明 portlet 已被成功安裝的消息。
將 myjspportal portlet 添加到 home - welcome 頁面:
單擊屏幕左上角帶有 portal administration 條目的下拉菜單。
選擇 work with pages。
單擊 page group 下拉菜單并選擇 home。
單擊 page 下拉菜單并選擇 welcome。
單擊屏幕上方的 get portlets
選擇 show all portlets 并單擊 go。
現在您將能看見一個已部署好的 portlet 的列表。向下滾動并找到 myjspportal portlet。單擊該行最左一欄的 + 符號選擇 myjspportal portlet 進行部署。
向上滾動頁面并單擊頁首左上端的 ok。
一旦你返回到原先的頁面,找到并突出顯示 myportal portlet。單擊 + 符號將 myportalportlet 添加到 welcome 頁面。
最后,單擊屏幕左下端的 activate 圖標激活添加了 myportal portlet 的 welcome 門戶網站頁面.
查看 home 頁面,看看您所作的更改:
找到并單擊左上角的下拉菜單并選擇查看 home 頁面。