Liferay 7 not able to set the global session attri

2020-07-25 08:55发布

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

2条回答
Summer. ? 凉城
2楼-- · 2020-07-25 09:26

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.

查看更多
forever°为你锁心
3楼-- · 2020-07-25 09:44

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!

查看更多
登录 后发表回答