2013-10-24 25 views
1

在我web.xml,主要配置如下:爲什麼rest-servlet,xml在SpringMVC中是必須的?

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value> 
     /WEB-INF/spring-mvc-config.xml 
    </param-value> 
</context-param> 

<filter> 
    <filter-name>SetCharacterEncoding</filter-name> 
    <filter-class> 
     org.springframework.web.filter.CharacterEncodingFilter 
    </filter-class> 
    <init-param> 
     <param-name>encoding</param-name> 
     <param-value>UTF-8</param-value> 
    </init-param> 
</filter> 

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

<servlet> 
    <servlet-name>rest</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
</servlet> 
<servlet-mapping> 
    <servlet-name>rest</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 

在我spring-mvc-config.xml,只有2條線:

<mvc:annotation-driven /> 
<import resource="spring.xml" /> 

而接下來,在我spring.xml,有春的所有東西配置,沒有任何關於springMVC配置。

當我開始這個web應用程序在Tomcat中,它總是拋出FileNotFoundException[WEB-INF /休息-servlet.xml中]後,我添加它,它只是正常工作。

我只想知道web.xml中的哪一部分指示rest-servlet.xml是WEB-INF目錄中的必備項。

我已經搜索了一下,但什麼都沒發現。任何人都可以幫我嗎?非常感謝!

回答

7

您已命名爲DispatcherServlet「rest」,因此默認情況下Spring MVC正在尋找rest-servlet.xml。如果您想使用不同的文件名,請執行以下操作:

<servlet> 
    <servlet-name>rest</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>classpath:/META-INF/spring/spring-mvc-config.xml</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 
+1

+1您可以從相關問題中瞭解「原因和方式」:http://stackoverflow.com/q/7833767/839646 –