gpt4 book ai didi

Liferay 7 无法设置全局 session 属性

转载 作者:行者123 更新时间:2023-12-02 10:07:56 25 4
gpt4 key购买 nike

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

设置 session 属性:

@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 中的 session 属性:

@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 都是具有不同 session 的不同上下文。您试图在一个 portlet session 中保存数据并在其他 portlet session 中恢复它,这是不正确的。 Liferay提供了获取portal全局session的方法:

        PortalSessionThreadLocal.getHttpSession();

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

关于Liferay 7 无法设置全局 session 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44621890/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com