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

时间:2017-06-19 03:32:44

标签: liferay liferay-7

我正在尝试设置Session属性[HTTP或Portlet Session],以便我可以全局访问它(通过门户网站)。但是在获取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);
    }
}

2 个答案:

答案 0 :(得分:3)

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

        PortalSessionThreadLocal.getHttpSession();

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

答案 1 :(得分:2)

我们遇到了与使用LR7.0相同的问题。我不确定这是错误还是其他什么。但作为解决方法,我们做了什么。我们正在进行原始会议。

HttpServletRequest httpRequest = PortalUtil.getOriginalServletRequest(PortalUtil.getHttpServletRequest(renderRequest)); HttpSession session = httpRequest.getSession(); session.setAttribute(" testAttr""喜&#34);

希望有所帮助!