gpt4 book ai didi

java - FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance()) 返回 null

转载 作者:行者123 更新时间:2023-11-30 05:58:06 24 4
gpt4 key购买 nike

我正在使用 Spring、Hibernate 和 JSF。

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

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

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

我需要在任何地方定义任何东西吗?

已编辑

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

最佳答案

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

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

关于java - FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance()) 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4786183/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com