在Liferay的不同页面上使用JSR-268 IPC for portlet

时间:2012-01-05 11:44:47

标签: liferay portlet jsr286

我开始使用WebSphere Portal开发基于portlet的应用程序,现在我将我的开发环境切换到Liferay。 我正在使用JSR-286引入的事件系统进行portlet间通信,试图避免所有非标准化的功能,以便将WebSphere Portal和Liferay作为支持的环境提供服务。

如果发布portlet和接收portlet在同一页面上,我的事件似乎工作正常,但我想将这些portlet放在不同的页面上。在WebSphere上有一个“连线”配置页面,其中可以配置portlet以将事件发送到其他页面上的特定portlet,并且可以选择在触发此类事件时自动切换页面。

如何使用Liferay做到这一点?

使用:Liferay Portal Community Edition 6.1.0 CE(Paton / Build 6100/2011年12月15日)

2 个答案:

答案 0 :(得分:2)

您可以在portal-ext.properties中设置一些属性。引用他们的评论(因为他们可能比我自己更好地陈述用法:

##
## Portlet Coordination
##

#
# Set this property to specify how events are distributed. If the value is
# "layout-set", then events will be distributed to all portlets contained in
# a layout set. If the value is "layout", then events will be distributed to
# all portlets that are present in a layout.
#
portlet.event.distribution=layout

#
# Set this property to specify how public render parameters are distributed.
# If the value is "layout-set", then public render parameters will be
# distributed to all portlets contained in a layout set. This will only work
# correctly if the property "layout.default.p_l_reset" is set to false. If
# the value is "layout", then public render parameters will be distributed
# to all portlets that are present in a layout.
#
portlet.public.render.parameter.distribution=layout

.....

#
# Set the default value for the "p_l_reset" parameter. If set to true, then
# render parameters are cleared when different pages are hit. This is not
# the behavior promoted by the portlet specification, but is the one that
# most end users seem to prefer.
#
layout.default.p_l_reset=true

希望有所帮助

答案 1 :(得分:1)

奥拉夫的回答为你提供了一个良好的开端。只需在类路径中放置一个名为portal-ext.properties的文件,其内容为portlet.event.distribution=ALL。这将确保所有关心事件的portlet都会收到它,即使它位于不同的页面上。

现在切换页面:我建议创建一个处理事件的界面。该接口基本上是代码中portlet.xml文件的event-definition-tags的表示。这还有一个好处,就是您必须确保您的接口和portlet.xml同步。如果接口与剩余的源代码不同步,那么这通常是编译时错误而不是运行时错误(例如事件的错误参数类型)。

interface Events
{
  public static final String EVENT_NAME_X ="eventX"; // as defined in portlet.xml
  public static final String EVENT_NAME_Y ="eventY";
  public void fireEventX(ActionResponse response, ParamType param);
  public void fireEventY(ActionResponse response, ParamType param);
}

然后,您可以使用一个简单的实现来触发可以与WebSphere一起使用的事件:

public class SimpleEvents implements Events
{
    @Override
    public void fireEventX(ActionResponse response, ParamType param)
    {
        response.setEvent(EVENT_NAME_X, param);
    }

    @Override
    public void fireEventY(ActionResponse response, ParamType param)
    {
        response.setEvent(EVENT_NAME_Y, param);
    }
}

然后你可以为Liferay提供另一个看起来像这样的实现:

public class RedirectingEvents extends SimpleEvents
{
  private String eventXRedirect;
  private String eventYRedirect;

    @Override
    public void fireEventX(ActionResponse response, ParamType param)
    {
        super.fireEventX(param);
        if (eventXRedirect != null)
          response.sendRedirect(eventXRedirect);
    }

    @Override
    public void fireEventY(ActionResponse response, ParamType param)
    {
        super.fireEventY(param);
        if (eventXRedirect != null)
          response.sendRedirect(eventYRedirect);
    }
    // setters & getters
}

现在,如果您正在使用Spring IoC(我碰巧知道您这样做),那么您可以在application-context.xml文件中按如下方式配置实现:

<bean class="package.RedirectingEvents" primary="true">
  <property name="eventXRedirect" value="/page-after-X-event" />
  <property name="eventYRedirect" value="/page-after-Y-event" />
</bean>

以下是获取此xml代码段的“值”部分的方法:

打开liferay中的目标页面,在事件被触发后应该将用户重定向到该目标页面,同时使用适当的权限记录并点击Manage-&gt;页面页面顶部的菜单。在那里你可以设置一个“友好URL”。将您在Friendly-URL字段中输入的相同URL(不带不可更改的前缀)复制到上面的application-context.xml片段中。

在触发事件的类中,您可以只允许自动连接Events-interface并使用它:

@Controller
class Foobar
{
  @Autowired
  private Events portletEvents;
  @ActionMapping
  public void action(ActionRequest request, ActionResponse response)
  {
    portletEvents.fireEventX(someParam);
  }
  @EventMapping(Events.EVENT_NAME_Y)
  public void handleEventRequest(EventRequest request, EventResponse response)
  {
    Object value = request.getEvent().getValue();
    log.info("got Y event! Value is: " + value);
  }
}

如果您在WebSphere Portal上部署应用程序,只需使用以下内容轻松交换上面的xml代码段:

<bean class="package.SimpleEvents" primary="true" />

现在您有了一个解决方案,允许您跨页面发送JSR-286消息,同时切换页面,同时仍然能够在Liferay和WebSphere Portal上部署应用程序而无需更改代码(只是配置需要调整。)

相关问题