Liferay 7 not able to set the global session attribute

大城市里の小女人 提交于 2019-12-06 12:00:35

问题


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.

SETTING the Session Attribute:

@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);
}
}

GETTING the Session Attribute in Different 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);
    }
}

回答1:


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();

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.




回答2:


We had the same issue we are using LR7.0. I am not sure if that is bug or something. But as a workaround this what we did. We are getting the original session.

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

Hope that helps!



来源:https://stackoverflow.com/questions/44621890/liferay-7-not-able-to-set-the-global-session-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!