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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18569826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 09:05:53  来源:igfitidea点击:

Spring - No WebApplicationContext found: no ContextLoaderListener registered?

javaspringspring-mvc

提问by user2681868

I am getting the following error while trying to run a Spring project

尝试运行 Spring 项目时出现以下错误

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

Inspite of adding the listner on to my web.xml. I am still getting this error. Below is the listener I have added to my web.xml :

尽管将侦听器添加到我的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?

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

回答by shazin

Try like this

像这样尝试

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

回答by Prabhakaran Ramaswamy

Please remove the first "/" in the

请删除第一个“/”

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
      WEB-INF/HelloWebRedirect-servlet.xml
    </param-value>
</context-param>

回答by dyrkin

Instead of specifiying contextConfigLocation try to specify dispatcher servlet

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

choose your servlet name according this template: /WEB-INF/${servlet-name}-servlet.xml

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

回答by Pulkit

Hi @user2681868 I was also facing the same problem here are the steps you should follow.

嗨@ user2681868 我也面临同样的问题,这里是您应该遵循的步骤。

1) in web.xml define this

1) 在 web.xml 中定义这个

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

2) create a applicationContext.xml in web-inf with this content

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>

回答by searching9x

If you using Annotation:

如果您使用注解:

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

}