No WebApplicationContext found: no ContextLoaderListener registered的解決方法

   <context-param>      

     <param-name>contextConfigLocation</param-name>   

 

      <param-value>classpath*:applicationContext.xml</param-value>   

 

   </context-param> 

 

我們在創建一個小工程的時候往往會碰見 No WebApplicationContext found: no ContextLoaderListener registered 這個異常  於是就在網上搜了一下 在web.xml.中加上了 這個

 <context-param>      

     <param-name>contextConfigLocation</param-name>   

 

      <param-value>classpath*:applicationContext.xml</param-value>   

 

   </context-param>

 

可是今天我加上這個也不管用了  ,於是我就再搜搜了一下  終於找到了 問題的所在 是因爲spring沒正常工作  在web.xml中

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

 

加上這個 就好了

 

 

下面就介紹一下 :(以後有待研究)

spring中提供 ContextLoaderListenter類,用來加載context的xml文件。

spring爲struts提供ContextLoaderPlugIn類,此類也可以加載context的xml文件。

區別在於,兩種方式加載的WebApplicationContext,以不同的Key存放在ServletContext中。而如果你定義了HibernateFilter的話,spring會利用WebApplicationContextUtils來獲取WebApplicationContext,而此類並不識別ContextLoaderPlugIn類所加載的上下文,此時便會拋出異常: No WebApplicationContext found: no ContextLoaderListener registered?

利用ContextLoaderListenter來加載dao、service級別的context,而對於struts的action,用ContextLoaderPlugIn加載。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章