Spring – 没有找到WebApplicationContext:没有注册ContextLoaderListener?

我在尝试运行Spring项目时遇到以下错误

HTTP Status 500 - java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered? 

暂时将列表器添加到我的web.xml 。 我仍然收到此错误。 下面是我添加到web.xml的监听器:

   contextConfigLocation /WEB-INF/HelloWebRedirect-servlet.xml    org.springframework.web.context.ContextLoaderListener   

有人可以在这方面帮助我吗?

嗨@ user2681868我也遇到了同样的问题,这是你应该遵循的步骤。

1)在web.xml中定义这个

  org.springframework.web.context.ContextLoaderListener  

2)使用此内容在web-inf中创建applicationContext.xml

     

试试这样吧

  org.springframework.web.context.ContextLoaderListener  

如果您使用注释:

 public class SpringWebAppInitializer implements WebApplicationInitializer { @Override public void onStartup(ServletContext servletContext) throws ServletException { AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext(); appContext.register(ApplicationContextConfig.class); ServletRegistration.Dynamic dispatcher = servletContext.addServlet("SpringDispatcher", new DispatcherServlet(appContext)); dispatcher.setLoadOnStartup(1); dispatcher.addMapping("/"); ContextLoaderListener contextLoaderListener = new ContextLoaderListener(appContext); servletContext.addListener(contextLoaderListener); // Filter. FilterRegistration.Dynamic fr = servletContext.addFilter("encodingFilter", CharacterEncodingFilter.class); fr.setInitParameter("encoding", "UTF-8"); fr.setInitParameter("forceEncoding", "true"); fr.addMappingForUrlPatterns(null, true, "/*"); } } 

请删除中的第一个“/”

  contextConfigLocation  WEB-INF/HelloWebRedirect-servlet.xml   

而不是指定contextConfigLocation尝试指定调度程序servlet

  HelloWebRedirect org.springframework.web.servlet.DispatcherServlet 1  

根据以下模板选择您的servlet名称:/WEB-INF/${servlet-name}-servlet.xml