Liferay 7共享的会话属性 [英] Liferay 7 Shared Session Attributes

查看:89
本文介绍了Liferay 7共享的会话属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Liferay共享会话属性.

I'm trying to use Liferay shared session attributes.

我想使用以下基于以下代码的代码,对不同URL的不同WAR文件中的两个不同portlet使用相同的属性:

I would like to to use the same attribute on two different portlets in different WAR file-s on different URL-s using the following code based on:

Liferay 7无法设置全局会话属性

我要保存的值:单个字符串

Value I want to save: A single string

在portlet 1中设置:

Setting in portlet 1:

String sharedKey = "LIFERAY_SHARED_" + key;
HttpSession session = PortalSessionThreadLocal.getHttpSession();
session.setAttribute(sharedKey, bean);

Portlet 1可以保留,重置和使用精细属性.

Portlet 1 is able to retain, reset and use attribute fine.

阅读portlet 2:

Reading in portlet 2:

key = "LIFERAY_SHARED_" + key;
HttpSession session = PortalSessionThreadLocal.getHttpSession();
Object bean = session.getAttribute(key);

此值始终为空.

两个portlet都是Spring MVC portlet.

Both portlets are Spring MVC portlets.

两个portlet都具有:

Both portlets have:

<instanceable>false</instanceable>
<private-session-attributes>false</private-session-attributes>
<requires-namespaced-parameters>false</requires-namespaced-parameters>

在他们的liferay portlet XML-s中.

In their liferay portlet XML-s.

这两个portlet都扩展了org.springframework.web.portlet.DispatcherPortlet.

Also both portlets extend org.springframework.web.portlet.DispatcherPortlet.

Liferay版本:

Liferay version:

Liferay DXP Digital Enterprise 7.0.10 GA1

Liferay DXP Digital Enterprise 7.0.10 GA1

任何帮助将不胜感激. 让我知道是否有人需要澄清.

Any help would be greatly appreciated. Let me know if anyone needs any clarification.

非常感谢, 朋友

推荐答案

Kyle Stiemann最近写了前缀的HttpSession,但是您应该使用Portlet会话:这是Liferay所管理的,HttpSession可能是模拟的",例如它可能不是tomcat管理的对象.

Kyle Stiemann has recently written a nice article on using sessions in portlets. TL;DR: You're using the HttpSession with an attribute prefixed "LIFERAY_SHARED_", but you should use the portlet session: That's what Liferay manages, the HttpSession might be "simulated", e.g. it might not be the object that tomcat manages.

引用他文章中的一种选择:

To quote one of the options from his article:

使用Liferay session.shared.attributes前缀(例如 LIFERAY_SHARED_)之间共享一个或多个会话属性 不同应用程序/WAR中的portlet.

Use Liferay session.shared.attributes prefixes (such as LIFERAY_SHARED_) to share one or more session attributes between portlets in different applications/WARs.

Liferay根据以下内容向所有portlet公开某些会话属性 某些前缀值.尽管可以通过以下方式配置这些前缀 portal-ext.properties,我建议使用默认前缀之一: LIFERAY_SHARED _.

Liferay exposes certain session attributes to all portlets based on certain prefix values. Although these prefixes are configurable via portal-ext.properties, I recommend using one of the default prefixes: LIFERAY_SHARED_.

例如:

// Portlet A 
portletRequest.getPortletSession(true)
    .setAttribute("LIFERAY_SHARED_" + CONSTANTS.ATTR_NAME, "value", 
                  PortletSession.APPLICATION_SCOPE);

// Portlet B (in a different WAR) 
String attrValue = portletRequest.getPortletSession(true)
    .getAttribute("LIFERAY_SHARED_" + CONSTANTS.ATTR_NAME, 
                  PortletSession.APPLICATION_SCOPE);

优点:

  • 仅将必要的属性公开给其他Portlet(而不是公开整个会话).

缺点:

  • 将会话属性公开给所有portlet.
  • 紧密耦合,而没有指示其他portlet可能正在使用此数据.
  • 共享会话数据的非标准方法.
  • Exposes session attribute(s) to all portlets.
  • Tight coupling without indicating which other portlets might be utilizing this data.
  • Non-standard method of sharing session data.

请注意,强烈建议仅使用基本类型作为会话属性.消除了对自定义序列化和类加载问题的需求.另外请注意,这里需要getPortletSession的变体,该变体具有附加的scope参数.

Note the strong recommendation to use only primitive types as session attributes. Eliminate the need for custom serialization and classloading problems. Also note that the variant of getPortletSession with the additional scope parameter is required here.

但是,尽管从技术上讲,它可以为您的问题提供答案,但您也想阅读.

But, as much as this technically provides an answer to your question, you also want to read "Session Storage is Evil".

TL; DR:请勿使用上述技术.而是消除会话的使用.

这篇关于Liferay 7共享的会话属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆