Liferay 7无法设置全局会话属性 [英] Liferay 7 not able to set the global session attribute

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

问题描述

我试图设置Session属性[HTTP或Portlet Session],以便可以全局访问(通过门户).但是,在获取Session属性的同时,它返回的是null而不是实际值.

I am trying to set the Session attribute [HTTP or Portlet Session] so that i can access it globally (through out the portal). But while getting the Session attribute its returning me the null instead of actual value.

@Component(
immediate = true,
property = {
    "com.liferay.portlet.display-category=IPC Sender",
    "com.liferay.portlet.instanceable=true",
    "javax.portlet.display-name=IPC_Sender Portlet",
    "javax.portlet.init-param.template-path=/",
    "javax.portlet.init-param.view-template=/view.jsp",
    "com.liferay.portlet.private-session-attributes=false",
    "javax.portlet.resource-bundle=content.Language",
    "javax.portlet.security-role-ref=power-user,user"
},
service = Portlet.class
)

public class ipcsenderPortlet extends MVCPortlet {

public void hello(ActionRequest actionRequest,
        ActionResponse actionResponse) throws Exception 
{   
//Trying to set HttpSession but its also getting null while retrieving
HttpServletRequest httpreq = PortalUtil.getHttpServletRequest(actionRequest);
HttpSession session = httpreq.getSession(true);
session.setAttribute("transfer", "content");

////Trying to set Portletsession but its also getting null while retrieving
PortletSession portletsession = actionRequest.getPortletSession();
portletsession.setAttribute("sendvalue","abcde", 
PortletSession.APPLICATION_SCOPE);
}
}

在其他Portlet中获取会话属性:

@Component(
    immediate = true,
    property = {
        "com.liferay.portlet.display-category=IPC Receiver",
        "com.liferay.portlet.instanceable=true",
        "javax.portlet.display-name=IPC_Receiver Portlet",
        "javax.portlet.init-param.template-path=/",
        "javax.portlet.init-param.view-template=/view.jsp",
        "javax.portlet.resource-bundle=content.Language",
        "com.liferay.portlet.private-session-attributes=false",
        "javax.portlet.security-role-ref=power-user,user"
    },
    service = Portlet.class
)
public class ipcreceiverPortlet extends MVCPortlet 
{
    public void doView(RenderRequest renderRequest, RenderResponse renderResponse) throws IOException, PortletException 
    {
        //HttpSession
         HttpServletRequest httpreq = PortalUtil.getHttpServletRequest(renderRequest);
         HttpSession session = httpreq.getSession();
         String name = (String)session.getAttribute("transfer");
         System.out.println("Session value through HttpSession:"+name);

         //PortletSession
         PortletSession portletsession = renderRequest.getPortletSession();
         String userName = (String) portletsession.getAttribute("sendvalue",PortletSession.APPLICATION_SCOPE);
         System.out.println("\nSession value through PortletSession:"+userName);
    }
}

推荐答案

这不是错误! Liferay是一个Portlet容器,在Portlet规范中,每个Portlet都是具有不同会话的不同上下文.您试图将数据保存在portlet会话中并在其他portlet会话中恢复它,这是不正确的. Liferay提供了一种获取门户网站全局会话的方法:

This is not a bug! Liferay is a portlet container and in the portlet specification every portlet is a different context with a different session. You are trying to save data in a portlet session and recover it in other portlet session, its not correct. Liferay provides a method to get the portal global session:

        PortalSessionThreadLocal.getHttpSession();

可以从门户网站的每个portlet检索此会话,但是BUT重要的是指定在集群环境中强烈建议不要在全局会话中保存数据,这主要是因为如果您从仅存在于portlet中的类中保存实例,则可以从不知道该类的其他Portlet获得ClassNotFoundException.仅建议使用全局会话来保存原始数据.

This session can be retrieved from every portlet of the portal, BUT is important specify that save data in the global session is strongly discouraged in clustered environments mainly because if you save a instance from a class that only exists in a portlet you can get ClassNotFoundException from other portlet that doesn't know that class. Global session is only recommended for save primitive data.

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

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