简体   繁体   中英

In Spring MVC, what is even the purpose of web.xml?

In Spring MVC, people typically build a Dispatcher Servlet that controls the other Servlets. The pipeline includes a request to web.xml which is then routed to a dispatcher of class org.springframework.web.servlet.DispatcherServlet . The URL pattern can be / or *.htm* to ensure that all requests go there.

The question is: in this pattern, what is even the purpose of web.xml ? One would think that it is just useless overhead. I mean, if you're not going to use another dispatcher... or are you?

Basically in a regular Java app context will be fetched in some self-created main method, means main method is your starting point. Application will run from the main and will go other methods after.

public class FooClass{
public static void main(String[] args) {
        //some code
    }

But in the Spring web app, the starting point is actually web.xml. It start from here, then flow goes to the other defined classes and methods

For example, when you write these codes, you basically give the order to the web application that you should start from here

Kind of you define your starting point. Think that it is main method in normal Java

<servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
         <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-mvc-validation-servlet.xml</param-value>
         </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

And in second part you give order to dispatcher that start from here. It means you give a url-pattern -starting point. You can give anything in here but "/" this is the common use

    <!-- Step 2: Set up URL mapping for Spring MVC Dispatcher Servlet -->
    <servlet-mapping>
            <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern> 
    </servlet-mapping>

I hope it is clear. Else ask for more explanations.

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