Judking
Judking

Reputation: 6371

Why rest-servlet,xml is a must in SpringMVC?

In my web.xml, the main configuration is as follows:

<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>

In my spring-mvc-config.xml, There are only 2 lines:

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

And next, In my spring.xml, there are all stuffs on spring configuration, without anything about springMVC configuration.

When I start this webapp in tomcat, it always throw an FileNotFoundException on [WEB-INF/rest-servlet.xml], After I add it, it just works fine.

I just want to know which part in web.xml instructs that a rest-servlet.xml is a must in WEB-INF directory.

I've googled about it but find nothing. Could anyone help me? Thanks a lot!

Upvotes: 2

Views: 7247

Answers (1)

MattSenter
MattSenter

Reputation: 3120

You have named the DispatcherServlet "rest," so by default Spring MVC is looking for rest-servlet.xml. If you want to use a different file name, do this:

<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>

Upvotes: 8

Related Questions