首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

通過RAP將Eclipse RCP應用遷移至瀏覽器運行完整實戰

     編輯:Java 相關知識

Eclipse RAP(Rich Ajax Platform)讓RCP(Rich Client Platform)應用幾乎零修改即可在瀏覽器中運行。本文通過將官方Mail模板從RCP遷移至RAP的完整過程,演示依賴替換、擴展點重構、啟動機制改造、主題品牌化等關鍵步驟。

遷移前準備

  • 創建RCP插件項目com.vwpolo.rcp.mail,選擇「RCP Mail Template」
  • 確認目標:將桌面端Mail Demo轉為Web端RAP Mail Demo

核心遷移步驟

1. 依賴替換(plugin.xml → Dependencies)

RCP原依賴RAP替換依賴
org.eclipse.uiorg.eclipse.rap.ui
org.eclipse.core.runtimeorg.eclipse.rap.ui.views

根據項目實際引用補充其他RAP bundles(如org.eclipse.rap.jface)。

2. 擴展點重構(plugin.xml → Extensions)

RCP擴展點RAP替換擴展點說明
org.eclipse.core.runtime.applicationsorg.eclipse.rap.ui.entrypoint啟動入口:IApplication → IEntryPoint
org.eclipse.core.runtime.productsorg.eclipse.rap.ui.branding品牌化:產品定義 → Servlet/主題/圖標/標題
org.eclipse.ui.bindings移除RAP不支持鍵綁定

3. 啟動類改造:IEntryPoint實現

原Application實現IApplication,改為實現IEntryPoint:

public class Application implements IEntryPoint {
    @Override
    public int createUI() {
        Display display = PlatformUI.createDisplay();
        return PlatformUI.createAndRunWorkbench(display, new ApplicationWorkbenchAdvisor());
    }
}

對比RCPstart():無需處理事件循環、display.dispose()、重啟邏輯,RAP容器自動管理生命周期。

4. 品牌化配置(org.eclipse.rap.ui.branding)

<branding
    id="com.vwpolo.rap.mail.branding"
    servletName="rapmail"
    defaultEntrypointId="com.vwpolo.rap.mail.entrypoint"
    themeId="org.eclipse.rap.ui.defaultTheme"
    title="RAP Mail Demo"
    favicon="icons/favicon.ico"
    body="loading.html"
    exitConfirmationClass="com.vwpolo.rap.mail.ExitConfirmation">
</branding>
  • servletName:瀏覽器訪問路徑/rapmail
  • body:加載頁面(如GMail風格「正在載入...」)
  • exitConfirmationClass:關閉瀏覽器彈出確認對話框

5. ActionFactory適配

RAP無ActionFactory.ABOUT,兩種方案:

  1. 自定義Action(推薦):
aboutAction = new Action() {
    public void run() {
        Shell shell = window.getShell();
        Bundle bundle = Platform.getBundle(MailActivator.PLUGIN_ID);
        String version = bundle.getHeaders().get(Constants.BUNDLE_VERSION);
        MessageDialog.openInformation(shell, "RAP MailDemo", "版本: " + version);
    }
};
aboutAction.setText("關於");
aboutAction.setId("com.vwpolo.rap.mail.about");
  1. 直接刪除About菜單項

驗證運行

  1. 右鍵項目 → Run As → RAP Application
  2. 瀏覽器訪問https://localhost:8080/rapmail
  3. 效果:完整Mail界面(郵件列表、文件夾樹、預覽面板)在Web中流暢運行

RAP Mail Demo截圖

遷移核心差異總結

維度RCPRAP
UI線程SWT事件循環RAP單線程模型(服務端)
客戶端原生OS控件HTML/JS(Qooxdoo/RWT)
會話管理單進程多用戶HTTP Session隔離
資源訪問本地文件系統ClassLoader/getResourceAsStream

遷移建議

  • 優先替換依賴與擴展點,再處理編譯錯誤
  • 利用PlatformUI.isWorkbenchRunning()區分RCP/RAP運行時邏輯
  • 自定義主題(CSS)適配Web風格,替換默認SWT主題
  • 大型項目分模塊漸進式遷移,保持雙端同步構建

完成上述步驟,RCP應用即可零成本擁抱Web部署優勢。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved