What is SpringMVC?
SpringMVC is also called Spring Web MVC. Is part of the Spring framework and was released after Spring3.0.
Spring MVC is gaining market share and has overtaken Struts2. This Springmvc tutorial covers the basics of Springmvc and its integration with the Spring framework (IoC container, AOP, etc.).
It is very easy to design clean Web layers and thin Web layers. Master powerful convention over configuration programming by contract support. Simple Web layer unit testing. It is easy to integrate with other view technologies, such as Velocity, FreeMarker, and so on, laying the foundation for later SSM projects.
Watch online: www.bilibili.com/video/BV1sk.
Data download: www.bjpowernode.com/javavideo/2…
For SpringMVC advantages
- Based on MVC architecture
Based on MVC architecture, the function division is clear. The coupling,
- Easy to understand, quick to handle; Simple to use.
You can develop an annotated SpringMVC project, SpringMVC is also lightweight and the JAR is small. Specific interfaces and classes that do not depend on.
- You can use Spring’s IoC and Aop as part of the Spring framework.
Convenient integration Strtus, MyBatis, Hiberate, JPA other framework.
- SpringMVC enhances the use of annotations for controllers, services, and DAOs.
Convenient and flexible. Use @Controller to create processor objects,@Service to create business objects, @AutoWired or @Resource to inject Service into Controller classes, and Dao into Service classes.
The first annotated SpringMVC application
Annotated development of SpringMVC means that classes and methods are annotated in the code to register the processor with the SpringMVC container. Annotated development is the focus.
Project: primary – the annotation
Complete function: the user submits a request, the server processor after receiving this request, gives a welcome message, display this information in the response page.
Create a New Maven Web project
pom.xml
After the Web project is created, add Servlet dependencies, SpringMVC dependencies
Rely on:
Plug-in:
Register with the central scheduler
(1) Fully qualified class name
The central scheduler is a Servlet named DispatcherServlet. The scheduler of restrictive category name in import the Jar file spring – webmvc – 5.2.5. The first Jar package org. Springframework. Web. Can be found under the servlet.
(2) < load-on-startup/>
The added effect in is to flag whether the Servlet instance is created when the Web server (Tomcat in this case) starts, that is, whether the init() method that executes the Servlet is called when the Web server starts, rather than when the Servlet is actually accessed.
Its value must be an integer.
When the value is greater than or equal to 0, it means that the container loads and initializes the servlet at startup. The smaller the value, the higher the priority of the servlet and the earlier it is created.
A value less than 0 or not specified indicates that the Servlet will not be created until it is actually used.
When the values are the same, the container selects its own creation order.
(3) < url-pattern/>
For, it can be written as /. *.do is recommended.
(4) Location and name of the configuration file
After the registration is completed, it can be published and run directly on the server. When accessing the browser page, the console throws a FileNotFoundException exception. By default, the configuration file named Servlet name-servlet.xml is found in the web-INF directory at the root of the project. The “Servlet name” here refers to the name value of the Servlet specified in the registration central scheduler tag. The configuration file name for this example is springmVC-servlet.xml.
Normally, configuration files are placed under the classpath, resources. So, when registering the central scheduler, you also need to look up the SpringMVC configuration file path and file name for the central scheduler Settings.
Open the source code for DispatcherServlet, which inherits from FrameworkServlet and has a contextConfigLocation property that sets the path and filename of the SpringMVC configuration file. This is where the attributes of the initialization parameter come from.
Create the SpringMVC configuration file
Create the SpringMVC configuration file springmVC.xml in the project’s classpath (SRC) directory. The file name can be any name.
Create handler
Add annotations to classes and methods. @Controller: indicates that the current class is a processor
@requestMapping: indicates that the current method is a processor method. This method handles and responds to the URI specified by the value attribute. The method name of the annotated method can be arbitrary.
If there are multiple request paths that match the execution of the handler method, you can write an array in the value attribute of @requestMapping.
The addObject() method in the ModelAndView class is used to add data to its Model. The underlying Model is a HashMap.
The data in the Model is stored in the request scope, and SringMVC jumps to the view by default by forwarding. The request ends and the data in the Model is destroyed.
Declare component scanner
Register the component scanner in springMVC.xml
Define the target page
Create a new subdirectory JSP under the WebApp directory and create a JSP page show.jsp.
Modify the view resolver registry
For SpringMVC framework in order to avoid the resource path for the request and the extension of redundancy, in view of the parser InternalResouceViewResolver and dropping out after dropping out before the introduction of the request. In ModelAndView, you only need to give the file name of the page to jump to. For the specific file path and file extension, the view parser will automatically complete the splicing.
Put the show. JSP file in the/web-INF/JSP/path
Modify processor
Use the logical view name, show is the logical view name.
Web request processing sequence using the SpringMVC framework
The MVC component of SpringMVC
SpringMVC executes the process
The flow chart
Perform simple process analysis
(1) The browser submits the request to the central scheduler
(2) The central scheduler directly forwards requests to the processor mapper.
(3) According to the request, the processor mapper will find the processor processing the request, encapsulate it as the processor execution chain and return it to the central scheduler.
(4) The central scheduler finds the processor adapter that can execute the processor according to the processor in the processor execution chain.
(5) The processor adapter calls the execution processor.
(6) The processor encapsulates the processing result and the view to jump into an object, ModelAndView, and returns it to the processor adapter.
(7) The processor adapter returns the results directly to the central scheduler.
(8) The central scheduler calls the view parser to encapsulate the view name in ModelAndView as a view object.
(9) The view parser returns the encapsulated view object to the central scheduler
(10) The central scheduler calls the view object and lets it render itself, that is, fill the data to form the response object.
(11) The central scheduler responds to the browser.