Spring Framework 02 Spring MVC

Now with SpringBoot, configuring SpringMVC does not need to use cumbersome xml files as before, and tomcat is also integrated internally, which is very convenient.

Quickly build Spring MVC projects

Use the official tutorial, enter the Spring official website, clickLearn-Guides

insert image description here

chooseServing Web Content with Spring MVC

image-20210727234817245

Follow the tutorial step by step, project structure

image-20210727235616357

Browser access 8080port to view the final effect

http://localhost:8080

image-20210727235844684

http://localhost:8080/greeting

image-20210727235750802

http://localhost:8080/greeting?name=User

image-20210727235804792

Spring Web MVC

Spring Web MVC is a Web framework originally built on top of the Servlet API, included in the Spring Framework from the beginning.

DispatcherServlet

Spring's Web MVC framework, like many other Web MVC frameworks, is request-driven, designed around a central servlet that dispatches requests to controllers and provides other functionality to facilitate Web application development. However, Spring's DispatcherServlet does much more than that. It is fully integrated with the Spring IoC container, thus allowing to use all the other features that Spring has. The request processing workflow of Spring Web MVC DispatcherServlet is shown in the figure below.

mvc

The DispatcherServlet is an actual Servlet (it inherits from the HttpServlet base class), so it is declared in the web application's web.xml. You need to map the requests you want the DispatcherServlet to handle by using URL mappings in the same web.xml file. The following shows such a DispatcherServlet declaration and mapping:

<web-app>
    <servlet>
        <servlet-name>example</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>example</servlet-name>
        <url-pattern>/example/*</url-pattern>
    </servlet-mapping>
</web-app>

All requests beginning with /example will be handled by a DispatcherServlet instance named example. In a Servlet 3.0+ environment, there is also an option to configure the Servlet container in code:

public class MyWebApplicationInitializer implements WebApplicationInitializer {
    
    
    @Override
    public void onStartup(ServletContext container) {
    
    
        ServletRegistration.Dynamic registration = container.addServlet("dispatcher", new DispatcherServlet());
        registration.setLoadOnStartup(1);
        registration.addMapping("/example/*");
    }
}

WebApplicationInitializer is an interface provided by Spring MVC that ensures that code-based configuration is detected and automatically used to initialize any servlet container. The abstract base class implementation of this interface is named AbstractDispatcherServletInitializer, which makes it easier to register the DispatcherServlet by simply specifying its servlet mapping. Then you need to configure the various beans used by the Spring Web MVC framework (on top of the DispatcherServlet itself). In the Web MVC framework, each DispatcherServlet has its own WebApplicationContext, which inherits all the beans already defined in the root WebApplicationContext. These inherited beans can be overridden at a servlet-specific scope, and you can define new scope-specific beans locally at a given servlet instance.

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-jDFKhSgT-1662031471583)(https://mypic-1305118058.cos.ap-hongkong.myqcloud.com/img /mvc-contexts%20-%20%E5%89%AF%E6%9C%AC.png)]

Implementing Controllers

Controllers typically define access to application behavior through service interfaces. Controllers interpret user input and transform it into models that are represented to users by views. Spring implements a controller in a very abstract way, and we can create all kinds of controllers.

Spring 2.5 introduced an annotation-based programming model for MVC controllers using annotations like @RequestMapping, @RequestParam, @ModelAttribute, etc. This annotation support is available for Servlet MVC and Portlet MVC. Controllers implemented in this style do not have to extend a specific base class or implement a specific interface.

@Controller
public class HelloWorldController {
    
    
    @RequestMapping("/helloWorld")
    public String helloWorld(Model model) {
    
    
        model.addAttribute("message", "Hello World!");
        return "helloWorld";
    }
}

The @Controller and @RequestMapping annotations allow flexible method names and signatures. In this particular example, the method accepts the model and returns the view name as a string, but various other method parameters and return values ​​can be used, as described later in this section. @Controller and @RequestMapping along with many other annotations form the basis of the Spring MVC implementation.

Filters

Browsers can HTTP GETonly HTTP POSTsubmit form data via or , but non-browser clients can also use HTTP PUT, PATCH, and DELETE. The Servlet API requires ServletRequest.getParameter()methods to support form field access for HTTP POST only. The spring-web module provides FormContentFilter to intercept HTTP PUT, PATCH, DELETE requests application/x-www-form-urlencodedwhose , read form data from the request body, wrap ServletRequest, and make ServletRequest.getParameter()the form data obtained through a series of methods.

Guess you like

Origin blog.csdn.net/wji15/article/details/126649845