In this article, we will quickly go through in understanding filename and its location of Spring-Dispatcher-Servlet in Spring MVC
- First we will understand, default configuration of the DispatcherServlet in Spring MVC
- Later will try to modify the filename and its location of the DispatcherServlet to user-defined in accordance with the project requirements
Default Spring-Dispatcher-Servlet in Spring MVC (in web.xml)
By default, the name of the dispatcher servlet is XXX-servlet.xml where XXX is the servlet name. In the below example, name of the servlet is ‘mvc-dispatcher’. And in this case, Spring container will by default loads the file named ‘mvc-dispatcher-servlet.xml’ from the location ‘/WEB-INF/’
Q: What if user/projects require having different filename in accordance with architecture and storing it in the different classpath location?
Answer: Move to the explanation 2
web.xml
<!-- Spring MVC DispatcherServlet: dispatches HTTP requests to registered controllers --> <servlet> <servlet-name>mvc-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>mvc-dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- location of the root application context xml file --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value> </context-param>
User-defined filename & location instead of default filename (in web.xml)
If you look at the below web.xml file, servlet name defined is ‘mvc-dispatcher’ but name of the file and its location are different from the default
For example in the below web.xml file, filename and its location which are defined in the contextConfigLocation parameter under <context-param> element is ‘WEB-INF/config/mvc-rest-dispatcher.xml’
This is different from the default filename that Spring container looks for while loading
web.xml
<!-- Spring MVC DispatcherServlet: dispatches HTTP requests to registered controllers --> <servlet> <servlet-name>mvc-rest-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>mvc-rest-dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- location of the root application context xml file --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/config/mvc-rest-dispatcher.xml</param-value> </context-param>
After deploying war into the Tomcat application server, spring container loads the servlet context resource from the context-param defined in the web.xml
Look at the below console, its loading the context resource i.e.; spring-dispatcher-servlet from the location ‘/WEB-INF/config/’ with filename ‘mvc-rest-dispatcher.xml’ appended to it. (/WEB-INF/config/ mvc-rest-dispatcher.xml)
Wait, something more to explain
What if project requires loading more than one context resources from different classpath location ?
Well, in that case you can simply add the complete classpath location of the files with comma-separated in the contextConfigLocation under <context-param> element
In the below example, we have got three files namely,
- WEB-INF/config/network.xml
- WEB-INF/customer.xml
- WEB-INF/config/inventory.xml
web.xml
<!-- Spring MVC DispatcherServlet: dispatches HTTP requests to registered controllers --> <servlet> <servlet-name>mvc-rest-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>mvc-rest-dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- location of the root application context xml file --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/config/network.xml, /WEB-INF/customer.xml, /WEB-INF/config/inventory.xml</param-value> </context-param>
Conclusion: Spring provides the capability to change the filename/location by overriding the default configuration. And this is pretty helpful depending on the project requirements and complying with the project architecture
Read Also:
- Spring MVC Framework
- Creating Maven-based project for Spring MVC web application in Eclipse Kepler IDE
- Spring MVC: Creating “Hello World” web application based on XML configuration
- Spring MVC: Annotation based “Hello World” web application
- Spring MVC: Multi-Controller actions using @Controller
- Spring MVC: Creating RESTful web service using annotation
- Spring MVC 4.0 Restful web service using @RestController annotation
Happy Coding !!
Happy Learning !!