Eclipse 中使用Pluto開發portlet

來源:互聯網
上載者:User
[pre]使用Eclipse plus Pluto開發你的第一個與JSR168相容的Portlet
By Terry.li
SpiritSeekerS@sqatester.com[/pre]

本文將介紹開發基於Portlet Specification v1.0 (JSR168) 的Portlet應用.  
我們使用Pluto作為Portlet Container和Portal. 同時可以將開發的Portlet應用遷移到任何支援JSR168的Portlet Container上.

·    為什麼要發布Portlet Specification? 什麼是JSR168? 
由于越來越多的公司開發了各自的Portal組件和基於其的Portal產品(如Bea, IBM, Oracle, Sun, Sybase, Novell, SAP, Jetspeed, Vignette 等.這種互不相容的介面實現不斷帶給程式供應商各種問題和麻煩, 為瞭解決這種問題, JCP發布了JSP168 (Java Specification Request), Portlet Specification, 用以提供不同Portal和Portlets之間的互用性

·    什麼是Portal?
Portal是基於WEB的應用程式, 它將不同資源進行整合并展現給使用者
通常其有如下三個特點:

a.    Personalization (個人化) 
b.    Single sign on (單點登陸)
c.    Content aggregation (內容彙總) 

其中Content aggregation指的是將不同來源的資訊整合到一個頁面中用來讓使用者更方便的進行使用. 
比如, 如果某客戶需要進行一次商業採購,以往需要訪問不同的供應商的首頁得到相關資訊, 但如果使用Portal將所有不同的貨物供應商的商品catalog頁面都整合到一個Catalog Portal頁面中, 那麼所有的商品資訊都可以更快的進行瀏覽,篩選和定貨, 加快了客戶的商業運作效率.

·    什麼是Portlet?
Portlet是一種基於WEB組件的JAVA技術, 由Portlet Container進行管理. 處理請求並動態返回頁面, 可以做為Portal 的可拔插的使用者介面組件.
 
·    什麼是Portlet Container? Portlet Container用來管理Portlet的生命週期並且提供其運行所需要的必要環境. 並且為Portlet Preferences提供持久性(Persistent)存取服務.但是其不支援內容的Aggregation. Aggregation由Portal組件提供.

注: Portlet Preferences是Portlet的一個新特性,提供類似資料庫的功能.但是不是用來取代資料庫. 只是用來存取簡單的Portlet參數配置.
        
·    什麼是WSRP?
WSRP 是 OASIS Web Service for Remote Portlets的縮寫. WSRP主要用來簡化Portal對於各種資源或者程式的整合的複雜性, 可以避免編程帶來的整合的複雜性. 並且Portal的管理員可以從大量的服務中選擇需要的用以整和到Portal中. 


·    Portlet and Servlet

摘自(Portlet Specs v1.0)

相同點:
• Portlets are Java technology based web components
• Portlets are managed by a specialized container
• Portlets generate dynamic content
• Portlets lifecycle is managed by a container
• Portlets interact with web client via a request/response paradigm

不同點:
• Portlets only generate markup fragments, not complete documents. The Portal aggregates portlet markup fragments into a complete portal page
• Portlets are not directly bound to a URL 
• Web clients interact with portlets through a portal system
• Portlets have a more refined request handling, action requests and render requests
• Portlets have predefined portlet modes and window states that indicate the function the portlet is performing and the amount of real state in the portal page
• Portlets can exist many times in a portal page

Portlet特有:
• Portlets have means for accessing and storing persistent configuration and customization data
• Portlets have access to user profile information
• Portlets have URL rewriting functions for creating hyperlinks within their content, which allow portal server agnostic creation of links and actions in page fragments
• Portlets can store transient data in the portlet session in two different scopes: the application-wide scope and the portlet private scope

 Servlet特有:
• Setting the character set encoding of the response
• Setting HTTP headers on the response
• The URL of the client request to the portal
  
·    什麼是Pluto ?
        Pluto 是 Apache開發下的一個Open Source項目, 是基於Portlet Specs的一個 Portlet Container 的實現. 它也提供了Sample Portal實現. 但是功能相對簡單:

      例如,
      1) 沒有複雜的Layout實現.
      2) 不是multi-user enabled, 比如, 不同User之間的Portlet Preferences互相是可以share的.   

     請記住Pluto只是一個Portlet Container的實現, 不是一個Portal的實現. 如果你需要功能更為強大的Portal,可以使用JetSpeed , 它同樣也是Apache的一個Open Source Project. 請參考文章末尾的資源部分.

·    概念 
      如圖: Figure 1.1
a.    Decorations and controls (修飾部分及 控制部分)
b.    Portlet fragment (Portlet 片段)
c.    Portlet window (Portlet 視窗)
d.    Portlet page (Portlet 頁面)

 
 
Figure 1.1

·    開發工具( Eclipse2.1, Pluto-plugin, Jakarta-tomcat-4.1.29)
下載地址:
Eclipse
http://www.eclipse.org/downloads/index.php

Pluto-plugin
http://prdownloads.sourceforge.net/plutoeclipse/org.eclipsefan.pluto.ui_1.0.0.zip?download

Tomcat4.1
http://mirrors.midco.net/pub/apache.org/jakarta/tomcat-4/v4.1.29/bin/jakarta-tomcat-4.1.29.zip

JDK1.4
http://Java.sun.com

·    配置開發環境
       Step1 :  解壓eclipse-SDK-2.1.1-win32.zip
     Step2 :  解壓org.eclipsefan.pluto.ui_1.0.0.zip , 並將其拷入eclipse/plugins目錄
下, 如下: eclipse/plugins/org.eclipsefan.pluto.ui_1.0.0/
/Icon
/Lib
/Source
/Webapp

        Step3:  解壓tomat4.1到一目錄. 並且配置好Tomcat伺服器.    
        Congratulations, It’s done!

a. 使用Portlet Wizard 來建立Portal
1) 選擇File > New > Project… > Portal > Pluto Portal application, 單          擊Next, 如圖:
  
Figure 1.2

2) 指定Project名稱 (如圖1.3), 這裡使用pluto, 然後單擊Next.
  
Figure 1.3




3) 指定Tomcat路徑, 單擊Finish.  之後eclipse會將Pluto安裝到tomcat上,並且自動設定好環境.
  
Figure 1.4




b. 建立Portlets Application


1) 建立Pluto portlet application (如圖Figure 1.5)
  
Figure 1.5

        2) 輸入project名稱 (如圖Figure 1.6), 這裡用portlets
          
Figure 1.6




      c. 最後產生的工程如圖 (Figure 1.7): 
 
Figure 1.7            
d. 通過portlet.xml 產生/更新 web.xml (如圖Figure 1.8)
         
Figure 1.8

e. 部署 Portlets

1) 編譯 portlets 工程.
2) 在 [your tomcat installation directory]/webapps下, 建立目錄portlets
3) 將portlets工程下的web-root目錄下所有檔案, 拷入以上建立的portlets目錄中.
Tip: 如果在每次修改後你厭倦了Ctrl+C 和 Ctrl+V, 你可以在Portlets工程目錄下自己編一個Bat檔案, 然後你可以在eclipse裡的Package Explorer中看到它, 雙擊就可以了. 如上圖(Figure 1.8): 其中deploy.bat檔案就是實現其功能的.

4) Start tomcat.
5) 在IE 中輸入: http://localhost: 8080/pluto/portal, 你應該能看到portal 頁面.


·    Sample Portlet
在Portlets工程檔案建立過程中, 自動產生許多檔案, 包括一個簡單的Portlet, 我們來觀察一下產生的sample portlet.


1) 與Servlet非常類似, Portlet擴充自GenericPortlet
    
    import javax.portlet.*;

public class SimplePortlet extends GenericPortlet

2) 其三個方法對應了Portlet 標題列中的三個聯結 (View, Edit, Help)

public void doView(…)

public void doEdit(…)

public void doHelp(…)

 
Figure 1.9

3) 三個方法分別調用了三個JSP檔案, 用以產生Portlet fragment, 同樣也可以調用Servlet產生Portlet fragment. 與Servlet應用類似,也可以使用
getInitParameter(String s)方法,得到設定檔中Portlet的初始值. 只不過Servlet使用web.xml,而Portlet 使用portlet.xml檔案.


portlet.xml

           <init-param>
            <name>jspView</name>
            <value>/jsp/view.jsp</value>
</init-param>


SimplePortlet.java

String jspName = 
getPortletConfig().getInitParameter("jspView");


·    Portlet Tag library

1)defineObjects Tag

如果開啟其中的一個JSP檔案,你會發現,和Servlet一樣,JSP中可以使用Portlet的一些variables.例如:

renderResponse
renderRequest
portletConfig

但是必須聲名:
<portlet:defineObjects/>
其中不可以定義任何屬性或者包含任何內容.

2) actionURL Tag
屬性    實值型別    對應值
windowState    String    minimized, normal, maximized
portletMode    String    view, edit, help
var    String    任何值
secure    String    true,false

<portlet:actionURL windowState=”normal” portletMode=”edit”>
<portlet:param name=”action” value=”login”/>
</portlet:actionURL>

建立一個action URL , 當訪問它時將使portlet window變為normal 裝態, 模式變為 edit.

3) renderURL Tag
屬性    實值型別    對應值
windowState    String    minimized,normal,maximized
portletMode    String    view, edit, help
var    String    任何值
secure    String    true,false

<portlet:renderURL portletMode=”view” windowState=”maximized”>
<portlet:param name=”number” value=”1”/> 
<portlet:param name=”page” value=”2”/>
</portlet:renderURL>

建立一個render URL , 當訪問它時將使portlet window變為maximized裝態, 模式變為 view.
 
4) namespace Tag
為目前的Portlet產生一個唯一的Value. 防止和其他的Portlet 或者Portal頁面上的value產生衝突.
<A HREF=”javascript: <portlet:namespace/>doFoo()”>Foo</A>

5) param Tag
屬性    實值型別
name    String
<portlet:param name=”myParam” value=”someValue”/>
注: param Tag不可以有body content.


總結
Pluto目前是v1.0, 有部分Portlet規範沒有完全實現, 例如, renderResponse.setTitle (), 同時你可以從Apache的CVS下載最新的Pluto代碼, 希望這編文章可以幫大家搭建一個簡單的Portlet開發環境 , 熟悉Portlet相關的知識.
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.