FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance())返回null [英] FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance()) returns null

查看:147
本文介绍了FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance())返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring,Hibernate和JSF.

I am using Spring, Hibernate and JSF.

为了从应用程序上下文中获取bean,我写:

In order to get a bean from application context I write:

public static Object findBean(String name) {
  return FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance()).getBean(name);
 }

但是,FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance())返回null,因此对其调用getBean()会引发NullPointerException

However, the FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance()) returns null, so calling getBean() on it throws NullPointerException

我需要在任何地方定义任何内容吗?

Do I need to define anything anywhere?

已编辑

我需要从应用程序上下文而不是jsf bean中获取bean

I need to get the bean from application context and not jsf bean

推荐答案

FacesContext.getCurrentInstance()如果不在FacesServlet处理的请求之内,则返回null.确保您正在处理通过该servlet传递的请求.

FacesContext.getCurrentInstance() returns null if your are not within a request that is handled by the FacesServlet. Make sure you are handling a request passed through that servlet.

更新:如果faces上下文不为空,则应用程序上下文可能为空的原因是,没有名为org.springframework.web.context.WebApplicationContext.ROOT的servlet上下文属性.这意味着弹簧未正确初始化.确保在web.xml中映射了弹簧ContextLoaderListener,或者在DispatcherServlet

Update: If the faces context is not null, the reason why the application context may be null is that there is no servlet context attribute named org.springframework.web.context.WebApplicationContext.ROOT. This means spring is not initialized properly. Make sure you have either the spring ContextLoaderListener mapped in web.xml, or the DispatcherServlet

这篇关于FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance())返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆