@H_502_1@我有一个bean,我最近从一个托管bean转变为一个spring-bean.
@H_502_1@一切都很好,直到某个时候调用以下方法:
@H_502_1@
Exception e = (Exception) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(
AbstractProcessingFilter.SPRING_SECURITY_LAST_EXCEPTION_KEY);
@H_502_1@事情发生了,因为FacesContext.getCurrentInstance()返回null.
@H_502_1@是否可以将faces上下文注入我的bean?最佳答案
@H_502_1@is it possible to inject the faces context into my bean?@H_502_1@不确定,但在这种特殊情况下,它不需要.
ExternalContext#getSessionMap()
基本上是HttpSession
属性的外观.到了这一点,你只需要以某种方式获取Spring bean中的HttpServletRequest
然后在HttpServletRequest#getSession()
之前从中获取HttpSession.然后你可以在HttpSession#getAttribute()
之前访问会话属性.
@H_502_1@我不做Spring,但Google告诉我你可以按如下方式获得它:
@H_502_1@
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
@H_502_1@完成后,您可以这样做:
@H_502_1@
Exception e = (Exception) request.getSession().getAttribute(AbstractProcessingFilter.SPRING_SECURITY_LAST_EXCEPTION_KEY);