调试老项目代码时,突然发现下面语句报错。
ServletContext application = ServletActionContext.getRequest().getSession().getServletContext();
WebApplicationContext ctx=WebApplicationContextUtils.getRequiredWebApplicationContext(application);
其中WebApplicationContextUtils.getRequiredWebApplicationContext(application);报错
java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered?
遇到这个问题的解决方案如下:
1 首先检查将自己spring的xml 文件放在WEB-INF目录下
2 其次在web.xml中检查两段话是否存在或被注释,经查我的web.xml中发现<listener>被注释掉导致此错误
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classconfig.xml,/WEB-INF/ldapcontext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
其中classconfig.xml和ldapcontext.xml是我的spring配置文件名。
3 重启tomcat