Portlet应用开发 (JSR168), Part 1

来源:百度文库 编辑:神马文学网 时间:2024/04/27 16:15:09

Portlet应用开发 (JSR168), Part 1

使用Eclipse plus Pluto开发你的第一个与JSR168兼容的Portlet

 

By Terry. Lee

SpiritSeekerS@sqatester.com

 

本系列将介绍如何开发基于Portlet Specification v1.0 (JSR168) 的Portlet应用程序以及相关的概念. 这里使用Pluto v1.0作为Portal/Portlet Container. 并且可以将开发完成的Portlet应用程序发布到任何遵循JSR168规范的其他Portlet Container和Portal Server上.

 

·   为什么要发布Portlet Specification 什么是JSR168

越来越多的公司开发了各自的Portal组件和基于其的Portal产品(如Bea, IBM, Oracle, Sun, Sybase, Vignette, Novell, SAP, Plumtree, Apache 等.这种互不兼容的接口实现带给软件开发商以及Web开发人员各种问题, 为了解决这些问题, JCP发布了JSP168 (Java Specification Request), Portlet Specification v1.0, 用以提供不同Portal和Portlets的实现之间的互通性.可能许多软件开发商 (如上所列) 提供更为强大的Portlet实现, 但是如果希望开发人员希望所开发的Portlet程序能够不依赖于某一种或者几种平台, 那么使用JSR168 Portlet毫无疑问是你的首选.

 

·         什么是Portal?

Portal是基于WEB的应用程序, 一个信息平台, 它将不同来源的各种资源进行整合并集中展现给客户.

 

通常其有如下三个特点:

 

a.                 Personalization (个性化)

b.                 Single sign on (单点登陆)

c.                  Content aggregation (内容聚合)

 

Personalization是Portal提供的特性之一, 用来提供用户的个性化设置.

Single sign on是J2EE的一个特性.其中Content aggregation是Portal比较有特色的特性, 它将不同来源的信息整合到一个同一个页面中, 使得用户可以更便捷, 更快速的进行某些商业应用.

 

这里举一个简单的商业应用的例子, 如果某一客户需要进行一次商业采购行为,以往需要访问不同的产品供应商的主页得到相关信息,这往往是一个耗时耗力的过程, 但如果使用Portal将所有经常使用的相关商品供应商的商品浏页面都整合到一个Portal页面中, 那么所有的供应商的商品都可以更快的被浏览,筛选, 加快了客户的商业运作效率.

 

·         什么是Portlet?
Portlet是一种基于WEB组件的JAVA技术, 由Portlet Container进行管理. 处理请求并动态返回页面, 可以作为Portal的可即插即用的界面组件.

 

·         什么是Portlet Container? Portlet Container用来管理Portlet的生命周期并且提供其运行所需要的必要环境. 并且给Portlet Preferences提供持久性(Persistent)存取服务.但是其不支持Portlet的aggregation(内容聚合). 内容聚合由Portal组件提供.这个概念需要弄清楚.

 

注: Portlet PreferencesPortlet的一个新特性,提供类似数据库的功能.但是不是用来取代数据库. 只能用来存取简单的Portlet配置参数.

       

·         什么是WSRP?
WSRP 是 OASIS Web Service for Remote Portlets的缩写. WSRP是Web service的一种新的商业应用, 一种新的标准, 主要用来简化Portal对于各种资源或者程序整合的复杂性, 可以避免编程带来的整合麻烦和问题. 而且Portal的管理员可以从海量的WSRP服务中选择需要的功能用以整和到目前使用的Portal中. 它有三种Roles:

1) Producer -> 提供Portlet
2) Consumer -> 使用Portlet
3) End User -> 最终用户

它的特点在于Producer将Consumer所需要的信息通过WSRP返回给Consumer,这些信息是相对的标记Fragment(片段),例如HTML,XHTML等, 直接可以镶入用户的Page中,而不用象Web service一样需要单独开发用户端接口.

再举个WSRP的商业应用的例子:

如果一个客户需要采购一些PC软件,那么这个客户通过互联网登陆某家PC软件代理供应商的网页,查询相关信息,但是做为软件代理公司,PC软件的高级使用指南及配置工具是非常缺乏或者是无法及时更新的,这样一来使得用户需要登陆软件开发商的站点上才能得到相关信息. 然后再回到代理商主页进行订购.

而比较理想的商业应用应该是代理公司可以整合软件开发商提供的使用指南或者配置工具(可以由开发商及时更新).但是如果使用XML API, 那么需要针对不同软件提供商开发不同的接口实现. 而使用WSRP可以将相关的信息及工具直接镶入到代理商的页面用以动态,及时提供给客户.

WSRP4J是Apache的WSRP标准实现, 请参考文章末尾的资源部分.

·   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 Spec (v1.0) (JSR168) 的一个 Portlet Container 的实现. 它也提供了 一个演示版的Sample Portal实现,用以进行Portlet Container的测试. 但是功能相对简单:

    
例如,
    1)
没有复杂的Layout实现.
    2)
不是multi-user enabled, 比如, 不同User之间的Portlet Preferences互相是  可以share的.  

  
请记住Pluto只是一个Portlet Container的实现, 不是一个Portal的实现. 如果你需要功能更为强大的Portal,可以使用JetSpeed或者其他功能更强大的Portal , 它同样也是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服务器.

     

              配置完成.


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)

     
: 如果每次对Portlet.xml文件进行修改后,比如添加新的Portlet , 必须更新 

web.xml ,生成对应的Wrap Servlet , 否则 Portlet无法在Portal中加载.

      

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文件就是实现Deploy.


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(…)

3) 三个方法分别调用了三个JSP文件, 用以生成Portlet fragment, 同样也可以调用Servlet产生Portlet fragment, 或者不调用JSP或者Servlet,直接在方法中得到PrintWriter,用最简单的pw.println(…)打印出内容,类似Servlet,如下:

 

 

PrintWriter pw=renderResponse.getWriter();

pw.println("Hello, Portlet!");

 

 

Servlet应用类似,也可以使用

getInitParameter(String s)方法,得到配置文件中Portlet的初始值. 只不过Servlet使用web.xml,Portlet 使用portlet.xml文件.

 

 

portlet.xml

 

        

            jspView

            /jsp/view.jsp

 

 

SimplePortlet.java

 

String jspName =

getPortletConfig().getInitParameter("jspView");

 

 

·   Portlet Tag library

 

1)defineObjects Tag

 

如果打开其中的一个JSP文件,你会发现,和Servlet一样,JSP中可以使用Portlet的一些variables.例如:

 

renderResponse

renderRequest

portletConfig

 

但是必须声名:

其中不可以定义任何属性或者包含任何内容.

 

2) actionURL Tag

属性

值类型

对应值

windowState

String

minimized

normal

maximized.

 

portletMode

String

view, edit , help

var

String

 

secure

 

String

true

false

 

 

创建一个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

 

1”/>

2”/>

 

创建一个render URL , 当访问它时将使portlet window变为maximized装态, 模式变为 view.

 

4) namespace Tag

为目前的Portlet产生一个唯一的Value. 防止和其他的Portlet 或者Portal页面上的value产生冲突.

doFoo()”>Foo

 

5) param Tag

属性

值类型

name

String

: param Tag不可以有body content.

 

 

总结

Pluto目前是v1.0, 有部分Portlet规范没有完全实现, 例如, renderResponse.setTitle (), 同时你可以从ApacheCVS下载最新的Pluto代码, 希望这编文章可以帮大家搭建一个简单的Portlet开发环境, 熟悉Portlet相关的知识.

 

 

资源:

·   Pluto
http://jakarta.apache.org/pluto

·   Pluto Mail List
http://news.gmane.org/gmane.comp.jakarta.pluto.user

·   WSRP Spec1.0
http://www.oasis-open.org/committees/tc_home.php?wg_abbrev=wsrp

·   Apache的WSRP实现 (WSRP4J)
http://ws.apache.org/wsrp4j/

·   Apache’s Portal, JetSpeed:
http://jakarta.apache.org/jetspeed/site/index.html

·   JSR 168:
http://www.jcp.org/en/jsr/detail?id=168

·   "Portlet 规范介绍" By Stefan Hepper 和 Stephan Hesmer

    • Part 1: Get your feet wet with the specification‘s underlying terms and concepts (August 2003)
    • Part 2: The Portlet API‘s reference implementation reveals its secrets (September 2003)