6

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

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

4

5 回答 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>
于 2014-01-29T20:47:02.620 回答
3

像这样试试

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> 
于 2013-09-02T09:32:41.337 回答
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, "/*");
    }

}
于 2016-11-19T15:47:11.510 回答
0

请去掉第一个“/”

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
      WEB-INF/HelloWebRedirect-servlet.xml
    </param-value>
</context-param>
于 2013-09-02T09:51:48.373 回答
0

尝试指定调度程序 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

于 2013-09-02T09:53:28.517 回答