Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Name of the Spring XML file in Spring MVC

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.

like image 878
e11438 Avatar asked Aug 30 '25 18:08

e11438


2 Answers

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

like image 135
Master Slave Avatar answered Sep 02 '25 06:09

Master Slave


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>
like image 41
barunsthakur Avatar answered Sep 02 '25 06:09

barunsthakur