简体   繁体   中英

Name of dispatcher servlet xml in Spring

There are many Qs and As on this matter but I have not seen an answer to why my project works. I started it out following a tutorial so it just worked. This is the declaration of my Spring Dispatcher Servlet in web.xml:

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

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

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

Then I have my servlet definition in a file in the same folder called dispatcher-servlet.xml . But I am not declaring in the web.xml that the definition is to be found in that particular file. How can the web app know that this is the correct file?

I have seen other default names of the file in answers but none like mine. Is dispatcher-servlet.xml a default name that Spring knows about and can find?

From the Spring MVC documentation:

Upon initialization of a DispatcherServlet, Spring MVC looks for a file named [servlet-name]-servlet.xml in the WEB-INF directory of your web application and creates the beans defined there, overriding the definitions of any beans defined with the same name in the global scope.

Since your servlet name is 'dispatcher' Spring looks for the file dispatcher-servlet.xml in the default location of your WEB-INF folder.

https://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM