繁体   English   中英

使用ExternalContext从HttpServletRequest中检索时,HttpSession为null(随机)

[英]HttpSession is null (randomly) when retrieved from HttpServletRequest using ExternalContext

我有以下代码在Icefaces(1.8.2) & JSF(1.2)的Liferay环境中使用ExternalContextHttpServletRequest检索HttpSession

ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
HttpServletRequest httpServletRequest = (HttpServletRequest) (externalContext.getRequestMap().get("com.liferay.portal.kernel.servlet.PortletServletRequest");
HttpSession httpSession = httpServletRequest.getSession();

上面的代码段对于Liferay 6_1_0_CE_GA1一直Liferay 6_1_0_CE_GA1因为始终填充httpSession

但是,在Liferay 6_2_1_CE_GA2的相同代码中,我随机将httpSessionnull 不知道有什么区别!

我也,试图让httpServletRequest使用PortalUtil.getOriginalServletRequest ,然后让httpSession从它,但这并不解决问题。

当我需要从Liferay Portlet访问HttpServletRequest时,必须使用以下命令:

HttpServletRequest httpServletRequest = PortalUtil.getOriginalServletRequest(PortalUtil.getHttpServletRequest(renderRequest));

renderRequest是从PortletRequest类扩展的对象。 从您的代码中,您可以通过以下方式获取PortletRequest对象:

PortletRequest portletRequest = (PortletRequest) externalContext.getRequest();

因此,结果代码应为:

PortletRequest portletRequest = (PortletRequest) externalContext.getRequest();
HttpServletRequest httpServletRequest = PortalUtil.getOriginalServletRequest(PortalUtil.getHttpServletRequest(portletRequest));

希望这可以帮助。 问候!

首先:请在6.2 GA6上试用,目前已经使用了一个月。 如果这是一个错误,则可能已经在以后的一个版本中修复了-您正在使用的版本已经快2年了。

此外,在规范中,我没有任何东西可以访问底层的HttpServletRequest 这并不是说这是不可能的(因为这显然是可能的),而是说行为可能会改变,因为您不在规格范围内。

我建议检查通过调试器获得的对象,并检查它们是否围绕原始servlet容器的请求或实际容器的对象进行包装。 另外,比较这些请求是定向到门户网站(检查URL)还是定向到portlet本身-因为它们是在不同的上下文中部署的,所以它们将具有不同的会话(或者一个可能具有会话,而另一个可能没有会话)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM