Rich Ajax Platform,第 1 部分: 簡介基於 Eclipse 開發模式的 Web 2.0 |
|
級別: 中級
Chris Aniszczyk ([email protected]), 軟件工程師, IBM
Benjamin Muskalla ([email protected]), 軟件工程師, Innoopract Informationssysteme GmbH
2007 年 12 月 03 日
作爲一種使基於 Web 的應用程序更加生動的方法,Asynchronous JavaScript + XML (Ajax) 和 Web 2.0 的概念已經在開發社區中廣泛應用。富 Ajax 平臺 (RAP) 是一種使用 Eclipse 開發模型來構建啓用 Ajax 的 Web 應用的方式。本文將介紹 RAP,說明如何設置 RAP 開發環境,提供一些演示以及一些易於理解的示例。
RAP 項目旨在使開發人員能夠使用 Eclipse 開發模型構建富 Internet 應用程序。“Eclipse 開發模型” 的確切含義是什麼?RAP 允許開發人員使用豐富的 Java™ 庫和 Eclipse API 構建基於瀏覽器的 Ajax 應用程序。它通過提供 SWT、JFace 和 Eclipse Workbench 的基於 Web 的實現來提供此項功能。本文的目的在於通過一些簡單示例介紹 RAP。
|
RAP 的設置非常簡單,只需要執行兩個步驟:下載 RAP 並配置 Eclipse 以使用 RAP。
從 Eclipse.org 獲得 RAP。建議您獲取最新的穩定里程碑版本。對於本文,我們使用的是 1.0 發行版。下載了包含 RAP 目標平臺的歸檔文件後,將其解壓縮到所選目錄中(參見圖 1)。此目錄十分重要,因爲這將是您在下一步中設置目標平臺時使用的目錄。
圖 1. RAP 目標
Eclipse Plug-in Development Environment (PDE) 將使用目標平臺的概念。目標平臺只是由一組表示待開發內容(也就是目標)的插件組成的。默認情況下,目標平臺被設爲針對當前運行的 Eclipse 實例。這意味着當前開發的插件應當運行在當前運行的實例中。目標平臺的靈活之處在於它可以被更改。例如,您可以將目標平臺設爲針對 Eclipse V3.2 安裝或者甚至其他產品的運行時(這種技巧使您不管開發什麼內容都可以使用最新的 Eclipse)。
在本例中,我們以 RAP 平臺爲目標,因爲我們要針對它進行開發。爲此,需要將目標平臺首選項 (Window > Preferences... > Plug-in Development > Target Platform) 設爲第 1 步中 RAP 插件的解壓縮目錄(參見圖 2)。
圖 2. 將 RAP 設爲目標平臺
現在您已經正確設置了目標平臺,可以開發 RAP 應用程序了。但是在開始查看代碼之前,讓我們先來看一個簡單演示,瞭解 RAP 可以做什麼並熟悉如何啓動基於 RAP 的應用程序。
|
|
體驗 RAP 的第一步是查看一個演示。要與 RAP 交互,需要創建一個啓動配置來啓動 RAP。爲此,需要打開 Run 對話框 (Run > Open Run Dialog...) 並創建一個新的 OSGi 框架啓動配置。完成後,需要確保這些 VM 參數已設置:-Dorg.osgi.service.http.port=8000 -Dosgi.noShutdown=true
(參見圖 3)。這些參數將使 RAP 可以在端口 8000 上啓動並阻止 Eclipse 在 RAP 啓動後立即關閉。
圖 3. RAP 啓動配置
最後,可以啓動瀏覽器並指向 http://localhost:8000/rap?startup=controls
來查看控件演示(參見圖 4)。該演示基於 SWT Examples 集合中著名的 ControlExample
。
圖 4. RAP 控件演示
|
我們將查看兩個示例,它們都是基於 PDE 所提供的一些 Rich Client Platform (RCP) 模板。
傳統的編程示例通常都是 Hello World 示例。我們首先將說明運行簡單 RAP 應用程序的方法。
圖 5. RAP 版 Hello World
RAP 版與 RCP 版的主要差別在於插件依賴性和應用程序入口點。如果查看插件清單(參見清單 1),我們可以看到具有不同的依賴性。
清單 1. RAP Hello World 依賴性 (MANIFEST.MF)
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Helloworld Plug-in
Bundle-SymbolicName: rap.helloworld; singleton:=true
Bundle-Version: 1.0.0
Bundle-Activator: rap.helloworld.Activator
Require-Bundle: org.eclipse.rap.ui
Eclipse-LazyStart: true
|
注意到對 org.eclipse.rap.ui
的依賴性了麼?那是類似於 RCP 庫中的標準 org.eclipse.ui
插件的 RAP 插件。如果熟悉 OSGi 和 Import-Package
文件頭,則根本無需依賴於特定的插件;您可以只依賴需要的包。這十分重要,因爲您可以用一種既能在 RAP 中工作又能在 RCP 中工作的方法來構造代碼。例如,如果查看 RAP(參見清單 2)和 RCP(參見清單 3)中的工作區顧問程序 (workbench advisor) 代碼,則會看到相似的代碼和導入包。
清單 2. RCP 工作區顧問程序
import org.eclipse.swt.graphics.Point;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;
public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {
public ApplicationWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) {
super(configurer);
}
public ActionBarAdvisor createActionBarAdvisor(IActionBarConfigurer configurer) {
return new ApplicationActionBarAdvisor(configurer);
}
public void preWindowOpen() {
IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
configurer.setInitialSize(new Point(400, 300));
configurer.setShowCoolBar(false);
configurer.setShowStatusLine(false);
configurer.setTitle("Hello RCP");
}
}
|
清單 3. RAP 工作區顧問程序
import org.eclipse.swt.graphics.Point;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;
public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {
public ApplicationWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) {
super(configurer);
}
public ActionBarAdvisor createActionBarAdvisor(IActionBarConfigurer configurer) {
return new ApplicationActionBarAdvisor(configurer);
}
public void preWindowOpen() {
IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
configurer.setInitialSize(new Point(400, 300));
configurer.setShowCoolBar(false);
configurer.setShowStatusLine(false);
configurer.setTitle("Hello RAP");
}
}
|
RAP 與 RCP 應用程序之間的另一個主要差別是入口點(類似於 Java 語言中的 main(String[] args)
方法)。在 RCP 中,我們有用於定義 IApplication
(注意,對於早於 Eclipse V3.3 的版本,此接口被稱爲 IPlatformRunnable
)的 org.eclipse.core.runtime.applications
擴展點。RAP 中與 RCP 應用程序等效的是 org.eclipse.rap.ui.entrypoint
擴展點,該擴展點將定義 IEntryPoint
。如果查看 RCP 上下文(參見清單 4)和 RAP 上下文(參見清單 5)中的典型入口點代碼,您會發現存在相似之處。在兩個例子中,我們都將創建一個視圖和工作區以運行工作區顧問程序。
清單 4. RCP 應用程序入口點 (org.eclipse.core.runtime.applications)
public class Application implements IApplication { public Object start(IApplicationContext context) throws Exception { Display display = PlatformUI.createDisplay(); try { int returnCode = PlatformUI.createAndRunWorkbench(display, new ApplicationWorkbenchAdvisor()); if (returnCode == PlatformUI.RETURN_RESTART) return IApplication.EXIT_RESTART; else return IApplication.EXIT_OK; } finally { display.dispose(); } } public void stop() { final IWorkbench workbench = PlatformUI.getWorkbench(); if (workbench == null) return; final Display display = workbench.getDisplay(); display.syncExec(new Runnable() { public void run() { if (!display.isDisposed()) workbench.close(); } }); } |
清單 5. RAP 應用程序入口點 (org.eclipse.rap.ui.entrypoint)
public class Application implements IEntryPoint { public Display createUI() { Display display = PlatformUI.createDisplay(); PlatformUI.createAndRunWorkbench( display, new ApplicationWorkbenchAdvisor() ); return display; } } |
著名的 RCP 郵件 示例(參見圖 6)在經過少量更改後能夠在 RAP 環境內運行(參見圖 7)。此示例表明視圖、透視圖、編輯器等工作區概念可以在 RAP 中使用(有關概念映射,請參見表 1)。您可以獲得郵件示例和先前示例的 源代碼 進行修改。
表 1. 比較 RAP 與 RCP
RAP | RCP |
---|---|
OSGi(服務器端) | OSGi |
Standard Widget Toolkit (SWT) | RAP Widget Toolkit (RWT) |
JFace | JFace |
工作區 | Web 工作區 |
圖 6. RCP 郵件示例
圖 7. RAP 郵件示例
|
本文通過幾個簡單的示例和演示介紹了 RAP。RAP 允許在 Eclipse 中重用您現有的技能並創建富 Internet 應用程序。RAP 還使您可以用能夠在臺式機 (RCP) 和瀏覽器 (RAP) 之間重用的方法構造代碼。實質上,RAP 現在將把 Eclipse 引入到瀏覽器和 Web 2.0 陣營中。
|
描述 | 名字 | 大小 | 下載方法 |
---|---|---|---|
樣例代碼 | os-eclipse-richajax.zip | 52KB | HTTP |
關於下載方法的信息 |
學習
- 您可以參閱本文在 developerWorks 全球站點上的 英文原文。
- 在 Eclipse.org 查閱關於富 Ajax 平臺 (RAP) 的更多信息。
- 查閱 RAP 維基 瞭解新功能和高級用例。
- 查閱 “Eclipse 推薦讀物列表”。
- 瀏覽 developerWorks 上的全部 Eclipse 文章和Eclipse 教程。
- 您是 Eclipse 新用戶?閱讀 developerWorks 文章 “Eclipse 平臺入門” 以瞭解它的起源和架構,以及如何用插件擴展 Eclipse。
- 查閱 IBM developerWorks 的 Eclipse 項目資源 擴展 Eclipse 技巧。
- 收聽針對軟件開發人員的有趣訪談和討論,一定要訪問 developerWorks podcast。
- 要獲得關於 Eclipse 平臺的介紹性文章,請參閱 “Eclipse 平臺入門”。
- 隨時關注 developerWorks 的 技術事件和網絡廣播。
- 查看免費的 developerWorks On demand demo 觀看並瞭解 IBM 及開源技術和產品功能。
- 查閱最近將在全球舉辦的面向 IBM 開放源碼開發人員的研討會、交易展覽、網絡廣播和其他 活動。
- 訪問 developerWorks 開放源碼專區,獲得豐富的 how-to 信息、工具和項目更新,幫助您用開放源碼技術進行開發,並與 IBM 產品結合使用。
獲得產品和技術
- 在 IBM alphaWorks 中查閱最新的 Eclipse 技術下載。
- 從 Eclipse Foundation 下載 Eclipse Platform 和其他項目。
- 下載 IBM 產品評估版本,並開始使用 DB2®、Lotus®、Rational®、Tivoli® 和 WebSphere® 的應用程序開發工具和中間件產品。
- 使用 IBM 試用軟件 改進您的下一個開發項目,這些軟件可以通過下載或從 DVD 中獲得。
討論
- Eclipse Platform 新聞組 應當是討論關於 Eclipse 的問題的第一站(選擇此鏈接將啓動默認的 Usenet 新聞閱讀器應用程序並打開 eclipse.platform)。
- Eclipse 新聞組 中有很多參考資料適用於對使用和擴展 Eclipse 感興趣的人員。
- 參與 developerWorks blog 並加入 developerWorks 社區。
Chris Aniszczyk 是 IBM Lotus 的軟件工程師,專門從事 OSGi 相關開發工作。他是開放源碼的忠實擁護者,目前在開發 Gentoo Linux 發行版,他還是一些 Eclipse 項目(PDE、ECF、EMFT)的參與者。他十分 樂意與您討論 關於開放源碼和 Eclipse 方面的問題。 |
Benjamin Muskalla 在位於卡爾斯魯厄的 Innoopract Informationssysteme 中擔任軟件開發人員兼顧問。他是富 Ajax 平臺 (RAP) 項目的提交者,主要負責工作區實現。他也是 Eclipse Platform |