加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

java – 将FacesContext注入spring bean

发布时间:2020-12-15 01:46:56 所属栏目:大数据 来源:网络整理
导读:我有一个bean,我最近从一个托管bean转变为一个spring-bean. 一切都很好,直到某个时候调用以下方法: Exception e = (Exception) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get( AbstractProcessingFilter.SPRING_SECURITY_LA

我有一个bean,我最近从一个托管bean转变为一个spring-bean.

一切都很好,直到某个时候调用以下方法:

Exception e = (Exception) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(
                    AbstractProcessingFilter.SPRING_SECURITY_LAST_EXCEPTION_KEY);

事情发生了,因为FacesContext.getCurrentInstance()返回null.

是否可以将faces上下文注入我的bean?

最佳答案

is it possible to inject the faces context into my bean?

不确定,但在这种特殊情况下,它不需要. ExternalContext#getSessionMap()基本上是HttpSession属性的外观.到了这一点,你只需要以某种方式获取Spring bean中的HttpServletRequest然后在HttpServletRequest#getSession()之前从中获取HttpSession.然后你可以在HttpSession#getAttribute()之前访问会话属性.

我不做Spring,但Google告诉我你可以按如下方式获得它:

HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

完成后,您可以这样做:

Exception e = (Exception) request.getSession().getAttribute(AbstractProcessingFilter.SPRING_SECURITY_LAST_EXCEPTION_KEY);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读