Spring-未找到WebApplicationContext:没有注册ContextLoaderListener?

7
我在尝试运行一个Spring项目时遇到了以下错误。
HTTP Status 500 - java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered?

尽管我已将监听器添加到我的web.xml中,但仍然出现此错误。以下是我在web.xml中添加的监听器:
 <context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>/WEB-INF/HelloWebRedirect-servlet.xml</param-value>
 </context-param> 

 <listener>
 <listener-class>
  org.springframework.web.context.ContextLoaderListener
 </listener-class>
 </listener> 

Can someone help me out in this regard?


我认为,引发异常的原因可能还有其他因素,您应该查看整个异常跟踪,特别是“caused by”。 - Sazzadur Rahaman
1
此外,我认为当<listener-class>没有在一行中定义并跨越三行时,这会导致问题。请注意,在声明这些内容时不要使用换行符。 - dharam
5个回答

16

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

1)在web.xml中定义这个。

<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener>

2)在WEB-INF目录下创建一个applicationContext.xml文件,其内容如下:

 <?xml version="1.0" encoding="UTF-8"?>
 <beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    </beans>

3
我看到的每个答案中,人们都提到了ContextLoaderListener,但没有提到applicationContext.xml本身。而你是第一个提到它的人。谢谢。 - Paulo
从ServletContext资源[/WEB-INF/applicationContext.xml]中的XML文档的第8行无效;嵌套异常是org.xml.sax.SAXParseException;行号:8;列号:126;cvc-elt.1:找不到元素'beans'的声明。我遇到了上述错误。 - Harsh Phoujdar

4

如果您使用注解:

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, "/*");
    }

}

3

试试这样

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> 

抱歉,我错过了什么吗??这不就是user2681868在他的web.xml中也有的内容吗!? - ParrapbanzZ

0
请移除该段代码中的第一个斜杠“/”。
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
      WEB-INF/HelloWebRedirect-servlet.xml
    </param-value>
</context-param>

0

不要指定 contextConfigLocation,而是尝试指定 dispatcher servlet。

<servlet>
     <servlet-name>HelloWebRedirect</servlet-name>
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
     <load-on-startup>1</load-on-startup>
</servlet>

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


1
我相信这并没有太大的区别。 - dharam
1
可以在Web应用程序中使用Spring而不使用Spring-MVC。如果OP尚未使用Spring-MVC,我建议他不要重构整个应用程序以使用它。 - NickJ

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接