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

时间:2013-09-02 09:29:28

标签: java spring spring-mvc

尝试运行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> 

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

5 个答案:

答案 0 :(得分:15)

嗨@ 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>

答案 1 :(得分:4)

试试这个

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

答案 2 :(得分:3)

如果您使用注释:

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 :(得分:0)

请删除

中的第一个“/”
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
      WEB-INF/HelloWebRedirect-servlet.xml
    </param-value>
</context-param>

答案 4 :(得分:0)

尝试指定dispatcher servlet

,而不是指定contextConfigLocation
<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