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

By Terry.lee

SpiritSeekerS@sqatester.com

 

 

         本部份将讲述PortletConfig对象及其PortletContext对象的概念及应用.

 

  • PortletConfig对象
    ServletConfig对象类似, PortletConfig对象提供Portlet初始的所需的参数及其对PortletContext对象存取提供相关方法.
    ServletConfig不同处在于, PortletConfig对象提供对Portlet Title Bar资源的I18N支持,我们可以设定不同的Resource Bundle文件用以提供多语言的支持, 如下portlet.xml文件:

 

… …

           <portlet-info>

                 <title>PortletConfig Example</title>

                 <short-title>PortletConfig</short-title>

                 <keywords>PortletConfig</keywords>

  </portlet-info>

  … …

 

以上Portlet描述文件中的设置用于显示PortletTitle Bar文字, 同样也可以使用Resource Bundle用以显示Title Bar文字, 如下:

 

  … …

  <resource-bundle>

portlets.portletconfig.portletconfigexample

</resource-bundle>

           … …

 

 

  • A case study

这里我们将开发一个简单使用Resource BundlePortlet.只需要添加所须的Resource Bundle文件.

 

我们使用英文及其中文的Resource Bundle, 如下:

 

3)    Base Resource Bundle (portletconfigexample.properties)

 

# English Resource Bundle

#

# filename: portletconfigexample.properties

# Portlet Info resource bundle example

javax.portlet.title=PortletConfig Example

javax.portlet.short-title=PortletConfig

javax.portlet.keywords=PortletConfig

2) Chinese Resource Bundle (portletconfigexample_zh.properties)

 

# Chinese Resource Bundle

#

# filename: portletconfigexample.properties

# Portlet Info resource bundle example

javax.portlet.title=Portlet配置例子

javax.portlet.short-title=Portlet配置

javax.portlet.keywords=Portlet配置

 

 

3) portlet.xml

 

… …

<resource-bundle>

portlets.portletconfig.portletconfigexample

</resource-bundle>

… …




      

 

  • 源代码及Portlet相关配置文件

    1) Portlet (
    PortletConfigExample.java)

 

package portlets.portletconfig;

 

/**

 * @author terry

 *

 * To change the template for this generated type comment go to

 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments

 */

 

import javax.portlet.*;

import java.io.IOException;

import java.io.Writer;

 

public class PortletConfigExample  extends GenericPortlet{

 

  public void doView(RenderRequest request, RenderResponse response)

  throws PortletException, IOException

  {

  response.setContentType("text/html");

  String view = getPortletConfig().getInitParameter("view");

  Writer writer=response.getWriter();

  writer.write(view);

  }

 

  public void doEdit(RenderRequest request, RenderResponse response)

  throws PortletException, IOException

  {

  response.setContentType("text/html");

  String edit = getPortletConfig().getInitParameter("edit");

  Writer writer=response.getWriter();

  writer.write(edit);

  }

 

}

 

 

2)     Portlet.xml

 

… …

       <!-- PortletConfig Example -->

       <portlet>

              <description>PortletConfig Example</description>

              <portlet-name>PortletConfigExample</portlet-name>

              <display-name>disPortletConfigExample</display-name>

              <portlet-class>portlets.portletconfig.PortletConfigExample</portlet-class>

              <init-param>

                     <name>view</name>

                     <value>Here is View Mode</value>

              </init-param>

              <init-param>

                     <name>edit</name>

                     <value>Here is Edit Mode</value>

              </init-param>

              <expiration-cache>-1</expiration-cache>

              <supports>

                     <mime-type>text/html</mime-type>

                     <portlet-mode>VIEW</portlet-mode>

                     <portlet-mode>EDIT</portlet-mode>

              </supports>

              <supported-locale>zh</supported-locale>

              <supported-locale>en</supported-locale>

              <resource-bundle>portlets.portletconfig.portletconfigexample</resource-bundle>

  </portlet>
… …

 

3)     pageregistry.xml

 

… …

              <!-- PortletConfig Example Page -->

    <fragment name="portletconfigpage" type="page">

        <navigation>

            <title>PortletConfig Example Page</title>

            <description>PortletConfig Example Page</description>

        </navigation>

        <fragment name="row1" type="row">

            <fragment name="col1" type="column">

                <fragment name="p1" type="portlet">

                    <property name="portlet" value="10.20"/>

                </fragment>

            </fragment>

        </fragment>

    </fragment>
… …

 

4)     PortletRegistry.xml

 

… …

                    <portlet id="20">

               <definition-id>portlets.PortletConfigExample</definition-id>

        </portlet>
… …

 

将以上源代码编译后, 再通过Eclipse生成/更新Portletweb.xml,  将所有配置及相关文件部署后, 启动Tomcat.

 

 

 

Browser中加载如下页面: Http://localhost:8080/pluto/portal , 可以看到如下的页面(:3-1)

 

如果机器的Locale及语言设定是以中文简体为缺省,则单击PortletConfig Example Page后可以看到如下Portlet 页面(3-1):

 

           3-1

 

: 因为现在Pluto的开发中没有做I18N的处理,这里如果你的机器的Locale是中文的话, 显示是乱码,请将IEencoding设定为GB2312(View -> Encoding -> Chinese Simplified), 如图3-1.

 

 

如果机器的Locale及语言设定是英文的话, 将看到如下Portlet 页面(3-2):

 

3-2

 

 

 

资源:

·         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

·         ApacheWSRP实现
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)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值