2011-10-09 128 views
16

在WEB-INFjava.lang.IllegalStateException:找不到WebApplicationContext:沒有ContextLoaderListener註冊?

<?xml version="1.0" encoding="UTF-8"?> 

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 

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

    <filter> 
     <filter-name>LoginFilter</filter-name> 
     <filter-class>glpi.filter.LoginFilter</filter-class> 
    </filter> 
    <filter-mapping> 
     <filter-name>LoginFilter</filter-name> 
     <url-pattern>/index.jsp</url-pattern> 
    </filter-mapping> 

    <servlet> 
     <servlet-name>context</servlet-name> 
     <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet> 
     <servlet-name>dispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

     <servlet> 
     <servlet-name>action</servlet-name> 
     <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> 
     <init-param> 
      <param-name>config</param-name> 
      <param-value>/WEB-INF/struts-config.xml</param-value> 
     </init-param> 
     <init-param> 
      <param-name>debug</param-name> 
      <param-value>2</param-value> 
     </init-param> 
     <init-param> 
      <param-name>detail</param-name> 
      <param-value>2</param-value> 
     </init-param> 
     <load-on-startup>2</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>dispatcher</servlet-name> 
     <url-pattern>*.htm</url-pattern> 
    </servlet-mapping> 
    <servlet-mapping> 
     <servlet-name>action</servlet-name> 
     <url-pattern>*.do</url-pattern> 
    </servlet-mapping> 
    <session-config> 
     <session-timeout> 
      30 
     </session-timeout> 
    </session-config> 
    <welcome-file-list> 
     <welcome-file>/login.jsp</welcome-file> 
    </welcome-file-list> 

</web-app> 
+0

可能重複[No WebApplicationContext found:no ContextLoaderListener registered?](https://stackoverflow.com/questions/8924761/no-webapplicationcontext-found-no-contextloaderlistener-registered) – BalusC

回答

20

此文件web.xml中,我認爲你缺少上下文加載器監聽器(來挑選你的Spring上下文文件(S))。

添加到您的web.xml

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

你也可以檢查出初始Web配置部分@http://static.springsource.org/spring/docs/2.0.x/reference/beans.html

+1

謝謝,但是當我添加上下文加載器監聽器,模塊:HELPDESKGESTION \ nbproject \ build-impl.xml:719:尚未部署。 – majda88

+3

很難評論,不知道發生了什麼。但是可以肯定的是你的問題在這裏應該由我的答案中的上面的代碼來解決。 – Saket

12

你既有ContextLoaderServlet和DispatcherServlet的設置時加載的啓動= 1.這意味着它們中的任何一個都可以首先啓動,並且您需要首先啓動ContextLoaderServlet,因爲這就是創建根錯誤所缺少的根WebApplicationContext的原因。因此請將ContextLoaderServlet的加載啓動時間設置爲1,並將DispatcherServlet更改爲2或更高。

實際上,最好使用ContextLoaderListener而不是Servlet,除非你在一個非常老的容器中,Listener無法正常工作。

+0

真實答案! +1 – gavenkoa

9

在web.xml文件中添加以下代碼,bcs會查找要加載的區域,因此我們必須首先聲明它。

<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

我最近偶然發現了同樣的問題,我知道肯定是不能被錯誤配置造成的,因爲我已經複製了整個從另一臺機器工作 Tomcat安裝。然而,我一直得到相同的異常:

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

正如我終於想通了,這是一個JVM版本錯誤,打破了應用程序:這個人用的Java 7,而工作實例(和web應用程序)在Java 8

希望它可以幫助有人與這種反直覺的錯誤消息掙扎。

+0

我可以問一下'this one'和'working instance'是什麼意思? – Stephane

+1

'This one'指的是給出錯誤的實例,'working instance'是另一臺機器上的源,我拷貝了整個事物(這工作正常)。 – yktoo