【问题标题】:Liferay 7 not able to set the global session attributeLiferay 7 无法设置全局会话属性
【发布时间】:2017-11-21 03:47:27
【问题描述】:

我正在尝试设置会话属性 [HTTP 或 Portlet 会话],以便我可以全局访问它(通过门户网站)。但是在获取 Session 属性时,它会返回 null 而不是实际值。

设置会话属性:

@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 liferay-7


    【解决方案1】:

    我们在使用 LR7.0 时遇到了同样的问题。我不确定这是错误还是什么。但作为一种解决方法,这是我们所做的。我们正在获取原始会话。

    HttpServletRequest httpRequest = PortalUtil.getOriginalServletRequest(PortalUtil.getHttpServletRequest(renderRequest)); HttpSession 会话 = httpRequest.getSession(); session.setAttribute("testAttr","hi");

    希望有帮助!

    【讨论】:

      【解决方案2】:

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

              PortalSessionThreadLocal.getHttpSession();
      

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

      【讨论】:

        猜你喜欢
        • 2018-01-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-21
        • 2013-09-21
        • 2016-02-20
        相关资源
        最近更新 更多