Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将FacesContext注入SpringBean_Java_Spring_Jsf - Fatal编程技术网

Java 将FacesContext注入SpringBean

Java 将FacesContext注入SpringBean,java,spring,jsf,Java,Spring,Jsf,我有一个bean,我最近把它从托管bean转换成了Springbean 在调用以下方法之前,一切正常: Exception e = (Exception) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get( AbstractProcessingFilter.SPRING_SECURITY_LAST_EXCEPTION_KEY); 此时,由于FacesCon

我有一个bean,我最近把它从托管bean转换成了Springbean

在调用以下方法之前,一切正常:

Exception e = (Exception) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(
                    AbstractProcessingFilter.SPRING_SECURITY_LAST_EXCEPTION_KEY);
此时,由于
FacesContext.getCurrentInstance()
返回空值,事情就变得一团糟了

可以将faces上下文注入到我的bean中吗

可以将faces上下文注入到我的bean中吗

不确定,但在这种特殊情况下不需要。基本上是一个外观的属性。在这一点上,您只需要以某种方式获取Springbean中的
HttpSession
。然后,您可以通过访问会话属性

我不做Spring,但我知道您可以通过以下方式获得它:

HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
完成后,您可以执行以下操作:

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

今天我遇到了完全相同的问题,所以我想把这个答案贴出来,以备将来参考

FacesContext可以通过以下方式注入:

@ManagedProperty("#{facesContext}")
FacesContext faces;
如果spring和JSF在应用程序中正确集成,它也适用于Springbean

参考:


您的咨询费率是多少?另外,你知道我如何获得托管bean吗?它们是否在会话中?因此,我很高兴获得投票和绿色分数。对于托管bean,它们被存储为
HttpServletRequest
(请求范围)、
HttpSession
(会话范围)和
ServletContext
(应用范围)的属性,以及它们的托管bean名称作为键。你知道,JSF(和Spring)只是在“原始”Servlet API上运行。只是作为背景:如果
FacesContext.getCurrentInstance()
返回null,那么调用可能是在JSF生命周期之外进行的(在上下文初始化之前、销毁之后,或者在不同的线程上)
FacesContext
实例在
ThreadLocal
变量中保留了每个请求的作用域。谢谢!我已经为此工作了10个小时,这两行代码处理了它。