Flow Diagram of Spring MVC
Img Source: TutorialsPoint
Purpose of web.xml
- Until Spring 3.1 the only way to configure the DispatcherServlet was with the WEB-INF/web.xml file.
- servlet name is dispatcher
- an instance of DispatcherServlet Class
- Will be initialized with a parameter named contextConfigLocation which contains the path to the configuration XML
- load-on-startup is an integer value that specifies the order for multiple servlets to be loaded.
- So if you need to declare more than one servlet you can define in which order they will be initialized. Servlets marked with lower integers are loaded before servlets marked with higher integers.
- With the servlet mapping we are bounding it by its name to a URL pattern that specifies what HTTP requests will be handled by it.
- The listener ContextLoaderListener is used to create root WebApplicationContext
Purpose of Servlet-Context.xml
- Component scanning with <context:component-scan base-package=”com.mycompany.maventestwebapp” /> is telling spring that it should search the classpath for all the classes under com.mycompany.maventestweapp and look at each class to see if it has a @Controller, or @Repository, or @Service, or @Component and if it does then Spring will register the class with the bean factory as if you had typed <bean class=”…” /> in the XML configuration files.
- <mvc:annotation-driven /> means that you can define spring beans dependencies without actually having to specify a bunch of elements in XML or implement an interface or extend a base class.
- <tx:annotation-driven /> The tx tags deal with configuring all of those beans in Spring’s comprehensive support for transactions.
- <bean class=”org.springframework.web.servlet.view.InternalResourceViewResolver”> In Spring MVC, InternalResourceViewResolver is used to resolve “internal resource view” (in simple, it’s final output, jsp or html page) based on a predefined URL pattern.
Purpose of Controller.java
- Controllers provide access to the application behavior that you typically define through a service interface. Controllers interpret user input and transform it into a model that is represented to the user by the view.
- The @Controller annotation indicates that a particular class serves the role of a controller.
- The @RequestMapping annotation is used to map URLs such as /hello onto an entire class or a particular handler method.
- HTTP method request method (“GET”, “POST”, etc.) or an HTTP request parameter condition.—-> @RequestMapping(method = RequestMethod.GET)