关于这个问题有很多问题和解答,但我还没有看到我的项目为何有效的答案。我是按照教程开始的,所以它很管用。这是我的 Spring Dispatcher Servlet 在 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>
然后我在同一文件夹中名为 dispatcher-servlet.xml
的文件中有了我的 servlet 定义。但是我没有在 web.xml 中声明可以在该特定文件中找到该定义。 Web 应用程序如何知道这是正确的文件?
我在答案中看到了文件的其他默认名称,但没有一个像我的。 dispatcher-servlet.xml
是 Spring 知道并可以找到的默认名称吗?
最佳答案
来自 Spring MVC 文档:
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.
因为您的 servlet 名称是“dispatcher”,Spring 在您的 WEB-INF 文件夹的默认位置查找文件 dispatcher-servlet.xml。
https://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html
关于java - Spring 中调度程序 servlet xml 的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45817276/