1. 程式人生 > >spring mvc 配置web.xml servlet.xml檔案配置以及出現異常的解決方案

spring mvc 配置web.xml servlet.xml檔案配置以及出現異常的解決方案

 java.io.FileNotFoundException: class path resource [beans.xml] cannot be opened because it does not  exist

Could not open ServletContext resource [/WEB-INF/***.xml] 這個異常是我們在配置時候經常出現的錯誤,他的出現一般是web.xml檔案路徑出了問題

加配置檔案一般有兩種,一種是

  1. <init-param>  
  2.             <param-name>contextConfigLocation</param-name
    >  
  3.             <param-value>/WEB-INF/spring-mvc.xml</param-value>  
  4.         </init-param> 

WEB-INF目錄下載入spring-mvc.xml檔案   ,這就寫死了路徑 ,必須在WEB-INF下才行。

另一種是在classpath下載入spring-mvc.xml檔案  

<param-value>classpath:servlet-mvc.xml</param-value>

因為classpath預設的路徑是src,也就是說,你應該把servlet-mvc.xml檔案放在src路徑下。

出現 java.io.FileNotFoundException: class path resource [beans.xml] cannot be opened because it does not exist異常一般就是因為servlet-mvc.xml檔案路徑問題。

還有一種可能就是servlet-mvc.xml檔案中還有其他的類如Interceptor攔截器等而並沒有在web.xml檔案中進行配置從而發生這個異常,

如果你是從網上cope的,那就要注意了。