posts - 12, comments - 19, trackbacks - 0, articles - 23
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

Portlet应用开发 (JSR168)(一)

Posted on 2006-08-10 20:19 毛里求斯的化石 阅读(798) 评论(0)  编辑  收藏 所属分类: portal相关

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.                 P ersonalization ( 个性化 )

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 Preferences Portlet 的一个新特性,提供类似数据库的功能.但是不是用来取代数据库. 只能用来存取简单的 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

 

         <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 相关的知识 .

 

 

资源 :

·   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

 


只有注册用户登录后才能发表评论。


网站导航: