Spring mvc how does it work
It provides the configurable form and success views, and an onSubmit chain for convenient overriding. Automatically resubmits to the form view in case of validation errors, and renders the success view in case of a valid submission. Following events happens when DispatcherServlet receives are request: The DispatcherServlet configured in web.
The DispatcherServlet finds the appropriate Controller with the help of HandlerMapping and then invokes associated Controller. Then the Controller executes the logic business logic if written by the programmer and then returns ModeAndView object to the DispatcherServlet.
It is the heart of the Spring Web MVC framework; this core component receives all requests to your application. For example, it allows you to plug in different existing or new adapters for a lot of tasks:. The request processing methods will interest us the most. Understanding the HTTP request, both locally during standard development, as well as remotely , is a critical part of understanding the MVC architecture.
It defines the service method that receives incoming requests and produces responses. This is the method that is eventually called on any request to the server, including a simple GET request. HttpServlet class is, as the name suggests, the HTTP-focused Servlet implementation, also defined by the specification. In more practical terms, HttpServlet is an abstract class with a service method implementation that splits the requests by the HTTP method type and looks roughly like this:.
Next, HttpServletBean is the first Spring-aware class in the hierarchy. In case of the requests to your application, the doGet , doPost , etc methods are called for those specific HTTP requests. FrameworkServlet integrates the Servlet functionality with a web application context, implementing the ApplicationContextAware interface. But it is also able to create a web application context on its own. As you already saw, the HttpServletBean superclass injects init-params as bean properties.
So, if a context class name is provided in the contextClass init-param of the servlet, then an instance of this class will be created as an application context. Otherwise, a default XmlWebApplicationContext class will be used. But you could change that easily. For example, if you need to configure your Spring Web MVC application with a Groovy-based application context, you could use the following configuration of DispatcherServlet in the web.
The same configuration may be done in a more modern Java-based way using the WebApplicationInitializer class. The HttpServlet. However, at the Spring MVC level of abstraction, method type is just one of the parameters that can be used to map the request to its handler.
And so, the other main function of the FrameworkServlet class is to join the handling logic back into a single processRequest method, which in turn calls the doService method:. Finally, the DispatcherServlet implements the doService method. Here, it adds to the request some useful objects that may come in handy down the processing pipeline: web application context, locale resolver, theme resolver, theme source etc.
Also, the doService method prepares input and output flash maps. The [servlet-name]-servlet. The InternalResourceViewResolver will have rules defined to resolve the view names. The following section will show you how to create your actual components, i. The DispatcherServlet delegates the request to the controllers to execute the functionality specific to it.
The Controller annotation indicates that a particular class serves the role of a controller. The RequestMapping annotation is used to map a URL to either an entire class or a particular handler method. You will define required business logic inside a service method. You can call another method inside this method as per requirement. Based on the business logic defined, you will create a model within this method. The render method first sets the response locale using the provided LocaleResolver instance.
During rendering, the ModelAndView object could already contain a reference to a selected view, or just a view name, or nothing at all if the controller was relying on a default view. Since both hello and login methods specify the desired view as a String name, it has to be looked up by this name. So, this is where the viewResolvers list comes into play:.
This is a list of ViewResolver instances, including our ThymeleafViewResolver provided by the thymeleaf-spring5 integration library. This resolver knows where to search for the views, and provides the corresponding view instances. You can also annotate the method itself with ResponseBody to specify that its result has to be transformed directly to an HTTP response:.
See the original article here. Thanks for visiting DZone today,. Edit Profile. Sign Out View Profile. Over 2 million developers have joined DZone. In this post, we take a look at how powerful the features included in Spring MVC can be used to great effect in a web application. Like Join the DZone community and get the full member experience. Join For Free. Note that the login method receives a domain object as an argument and returns a ModelAndView object: import org.
PostMapping; import org. Hence, running the application, you are likely to see the following information in the log: TomcatWebServer : Tomcat initialized with port s : http StandardService : Starting service [Tomcat] DispatcherServlet as the Heart of Spring MVC What we really want to do as developers of a web application is to abstract away the following tedious and boilerplate tasks and focus on useful business logic: mapping an HTTP request to a certain processing method parsing of HTTP request data and headers into data transfer objects DTOs or domain objects model-view-controller interaction generation of responses from DTOs, domain objects, etc.
FrameworkServlet FrameworkServlet integrates the Servlet functionality with a web application context, implementing the ApplicationContextAware interface.
DispatcherServlet contextClass org.
0コメント