I'm new to Spring MVC and when i'm going through tutorials in different tutorials they name spring XML file in different names. As examples "spring-web-servlet.xml", "dispatcher-servlet.xml", "spring-dispatcher-servlet.xml". Can somebody explain to me why is that.
What barunsthakur has answered is a means to change the default location of the spring configuration file by using the context param contextConfigLocation
. If this param is not specified, spring mvc expects the following
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
Consider the following DispatcherServlet Servlet configuration (in the web.xml file):
<web-app>
<servlet>
<servlet-name>golfing</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>golfing</servlet-name>
<url-pattern>/golfing/*</url-pattern>
</servlet-mapping>
</web-app>
With the above Servlet configuration in place, you will need to have a file called /WEB-INF/golfing-servlet.xml in your application
Your projects most likely use default configurtion and in this case you must pair the name of your configuration file, with the servlet-name of your DispatcherServlet
you can read more in the docs here
The name of spring xml file doesn't matter. You can name it anything(something semantic will be good) and need configure the web.xml using same name. for e.g if the filename is spring-dispatcher-servlet.xml
the add this entry in web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:META-INF/spring-dispatcher-servlet.xml
</param-value>
</context-param>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With