Java Spring-未找到WebApplicationContext:未注册ContextLoaderListener?

Java Spring-未找到WebApplicationContext:未注册ContextLoaderListener?,java,spring,spring-mvc,Java,Spring,Spring Mvc,我在尝试运行Spring项目时遇到以下错误 HTTP Status 500 - java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered? 在我的web.xml中添加listner。我仍然得到这个错误。下面是我添加到web.xml的侦听器: <context-param> <param-name>contextConfi

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

HTTP Status 500 - java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered?
在我的
web.xml
中添加listner。我仍然得到这个错误。下面是我添加到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> 

上下文配置位置
/WEB-INF/HelloWebRedirect-servlet.xml
org.springframework.web.context.ContextLoaderListener
有人能在这方面帮我吗?

试试这个

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

org.springframework.web.context.ContextLoaderListener
请删除列表中的第一个“/”


上下文配置位置
WEB-INF/HelloWebRedirect-servlet.xml

尝试指定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>

HelloWebRedirect
org.springframework.web.servlet.DispatcherServlet
1.

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

Hi@user2681868我也面临同样的问题,以下是您应该遵循的步骤

1) 在web.xml中定义

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

org.springframework.web.context.ContextLoaderListener
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>

如果使用注释:

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

}

我认为,可能还有另一个导致异常的原因,您应该查看整个异常跟踪,特别是“由”引起的。此外,我认为,如果没有在一行中定义异常,并且扩展到三行,则会导致问题。。请务必声明这些内容而不使用新行字符。我相信在web应用程序中使用Spring而不使用Spring MVC并没有多大区别。如果OP还没有使用SpringMVC,我不会建议他重构整个应用程序来使用它。我看到的每个答案都提到ContextLoaderListener,但不是applicationContext.xml本身。你是第一个。谢谢。来自ServletContext资源[/WEB-INF/applicationContext.XML]的XML文档中的第8行无效;嵌套异常为org.xml.sax.saxpasseeption;行号:8;栏目号:126;cvc elt.1:找不到元素“bean”的声明。我收到了上面的错误对不起,我是不是遗漏了什么??这不正是user2681868在其web.xml中所拥有的吗!?
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, "/*");
    }

}