java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered?

前端 未结 4 1717
野趣味
野趣味 2021-02-12 14:50

This the file web.xml in WEB-INF





        
相关标签:
4条回答
  • 2021-02-12 15:17

    I've recently stumbled upon the same issue, and I knew for sure it couldn't be caused by misconfiguration because I've copied the entire working Tomcat installation from another machine. Yet I kept getting the same exception:

    java.lang.IllegalStateException: No WebApplicationContext found: not in a DispatcherServlet request and no ContextLoaderListener registered?
    

    As I've eventually figured out, it was a wrong JVM version that broke the application: this one used Java 7, whereas the working instance (and the webapp) was on Java 8.

    Hope it helps someone struggling with this counter-intuitive error message.

    0 讨论(0)
  • 2021-02-12 15:21

    I think you are missing the context loader listener(to pick your spring context file(s)).

    Add this to your web.xml

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

    You could also check out the Initial web configuration section @ http://static.springsource.org/spring/docs/2.0.x/reference/beans.html

    0 讨论(0)
  • 2021-02-12 15:24

    You have both ContextLoaderServlet and DispatcherServlet set to load-on-startup = 1. That means either one of them could start first, and you need the ContextLoaderServlet to start first, since that's what creates the root WebApplicationContext that your error says is missing. So leave ContextLoaderServlet's load-on-startup at 1, and change the DispatcherServlet's to 2 or higher.

    Actually, it's preferred to use ContextLoaderListener instead of the Servlet unless you're on a really old container where the Listener doesn't work properly.

    0 讨论(0)
  • 2021-02-12 15:37

    Add following code in web.xml file, bcs it looks for contex to load so we have to declare it initially.

        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/HelloWeb-servlet.xml</param-value>
        </context-param>
    
        <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
    
    0 讨论(0)
提交回复
热议问题