2013-05-21 173 views
1

以下是我的配置文件我配置Spring配置不配置名稱查找

WEB-INF /班/ applicationContext.xml的,但它尋找 WEB-INF/applicationContext.xml的

<?xml version="1.0" encoding="UTF-8"?> 
<web-app id="starter_anil" version="2.4" 
     xmlns="http://java.sun.com/xml/ns/j2ee" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 

    <display-name>Anil-Spring</display-name> 
    <!-- Servlets --> 
    <servlet> 
     <servlet-name>mvc-dispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>WEB-INF/classes/applicationContext.xml</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>mvc-dispatcher</servlet-name> 
     <url-pattern>/*</url-pattern> 
    </servlet-mapping> 

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

我得到以下異常

EVERE: Context initialization failed 
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/applicationContext.xml] 

這裏是我的applicationContext

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> 

<beans> 
    <import resource="config/controllers.xml" /> 
    <!-- <import resource="conf/spring/services.xml" /> 
    <import resource="conf/spring/persistence.xml" /> 
    <import resource="conf/spring/daos.xml" /> 
    <import resource="conf/spring//interceptors.xml" /> --> 
</beans> 

這種情況的解決方案是

<?xml version="1.0" encoding="UTF-8"?> 
<web-app id="starter_anil" version="2.4" 
     xmlns="http://java.sun.com/xml/ns/j2ee" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 

    <display-name>Anil-Spring</display-name> 
    <!-- Servlets --> 
    <servlet> 
     <servlet-name>mvc-dispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>/WEB-INF/classes/applicationContext.xml</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>mvc-dispatcher</servlet-name> 
     <url-pattern>/*</url-pattern> 
    </servlet-mapping> 

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

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

警告:未找到與URI [/AnilsSpring/WEB-INF/jsp/hello.jsp HTTP請求映射在DispatcherServlet的名稱爲 「MVC-調度」 請指導線我一次

+2

把你的'applicationContext。在'WEB-INF' –

+0

WEB-INF/classes中xml'/applicationContext.xml中爲什麼不採取任何配置 –

+0

因爲春天查找它,默認情況下,在'WEB-INF'。或者做Majid發佈的內容,但這是不好的做法,因爲它不屬於那裏。 –

回答

1

爲什麼你applicationContext是在類中?正如它在評論中告訴的那樣,將它移動到WEB-INF目錄。這應該解決問題。

/classes目錄用於編譯的類,但不用於配置文件。

+0

離開它的理想告訴我爲什麼contextConfigLocation的尋找另一個路徑 –

+0

因爲默認查找路徑是/ WEB-INF目錄下。 – user

+0

您是否知道需要contextConfigLocation在春天mvc –

0

嘗試添加//WEB-INF/classes/applicationContext.xml

如果還是不行,請嘗試,而不是你的web.xml(前servlet的聲明)

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

java.io.FileNotFoundException:無法打開ServletContext的資源[/WEB-INF/mvc-dispatcher-servlet.xml]我得到這個以避免只有我上面配置初始參數 –

+0

爲什麼它serching/WEB-INF /的applicationContext。在配置方面的配置位置 –

-1

WEB-INF/classes中添加此/ applicationContext.xml對於此要求,必須將applicationContext.xml放在課程路徑中,例如

如果你使用的是maven,那麼把它放在src/main/resources文件夾中,否則放在src文件夾中。

這時你會發現它的WEB-INF內/類

+0

XML eventhogh是的,我使用maven它裏面只有 –

+0

爲什麼它serching /WEB-INF/applicationContext.xml的eventhogh在配置方面的配置位置的src/main/resources文件夾 - ravula的剛纔編輯 –

+0

網絡。 xml配置在這裏看到https://github.com/SpringSource/spring-webflow-samples/blob/master/webflow-showcase/src/main/webapp/WEB-INF/web.xml – Ruju

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

<web-app id="starter_anil" version="2.4" 
     xmlns="http://java.sun.com/xml/ns/j2ee" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 

    <display-name>Anil-Spring</display-name> 
     <!-- Servlets --> 
    <servlet> 
     <servlet-name>mvc-dispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/classes/applicationContext.xml</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>mvc-dispatcher</servlet-name> 
     <url-pattern>/*</url-pattern> 
    </servlet-mapping> 

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



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





</web-app> 

本作例外少啓動Tomcat上,但我有點困惑

0

如果這就是你絕對必須有它,試試這個:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app id="starter_anil" version="2.4" 
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 

    <display-name>Anil-Spring</display-name> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>classpath:applicationContext.xml</param-value> 
    </context-param> 

    <the-rest-of-your-stuff /> 
</web-app> 

您可能需要一個applicationContext.xml/,但我不這麼認爲。

0

可能您需要執行取消部署。並部署它。它會工作。 。 它適用於我的異常 IOException從ServletContext資源解析XML文檔[/ WEB-INF/classes/spring application config.xml];