(1) SpringMVC study notes (introduction + helloSpringMVC)

1. Spring MVC

1. What is MVC

MVC is a software architecture idea that divides software into models, views, and controllers.

M: Model, the model layer, refers to the JavaBean in the project, the role is to process data

JavaBean is divided into two categories:

  • One class is called Entity Bean: specifically stores business data, such as Student, User, etc.

  • One class is called business class Bean: refers to Service or Dao object, which is specially used to handle business logic and data access

V: View, view layer, refers to pages such as html or jsp in the project, which is used to interact with users and display data

C: Controller, control layer, refers to the servlet in the project, the role is to accept requests and respond to browsers

The workflow of MVC:

The user sends a request to the browser through the view layer, the request is received by the controller in the server, the controller calls the corresponding model layer to process the request, and returns the result to the controller after processing, the controller then finds the corresponding view according to the result of the request processing, and renders the data After the final response to the browser

2. What is spring MVC

springMVC is a follow-up product of Spring and a sub-project of Spring

SpringMVC is a complete set of solutions provided by Spring for the development of the presentation layer. After the presentation layer framework has undergone successive changes in many products such as Strut, WebWork, and Strut2, the industry generally chooses SpringMVC as the preferred solution for the development of the presentation layer of javaEE projects.

3. The characteristics of Spring MVC

  • Spring family products seamlessly interface with infrastructure such as IOC containers

  • Based on the native Servlet, through the powerful front controller DispatcherServlet, the request and response are processed uniformly

  • The problems that need to be solved in each subdivision of the presentation layer cover all aspects and provide all-round solutions

  • The code is fresh and concise, improving development efficiency

  • The degree of internal components is high, and the pluggable components are plug-and-play.

  • Outstanding performance, in line with Internet project requirements.

Two, Hello World

1. Development environment

2. Create a maven project

add web module

Packaging method war

Introduce dependencies

        <!-- SpringMVC -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.1</version>
        </dependency>

        <!-- 日志 -->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.3</version>
        </dependency>

        <!-- ServletAPI -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>

        <!-- Spring5和Thymeleaf整合包 -->
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf-spring5</artifactId>
            <version>3.0.12.RELEASE</version>
        </dependency>
    </dependencies>

3. Configure web.xml

Register SringMVC's front controller DispatcherServlet

  • Default configuration method:

Under this configuration, the configuration file of springMVC is located under WEB-INF by default, and the default name is <servlet-name>-servlet.xml. For example, the configuration file of SpringMVC corresponding to the following configuration is located under WEB-INF, and the file name is springMVC- servlet.xml

    <!--配置SpringMVC的前端控制器,对浏览器发送的请求进行统一处理-->
    <servlet>
        <servlet-name>SpringMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>SpringMVC</servlet-name>
        <!--
        设置springMVC的核心控制器所能处理的请求的请求路径
        /所匹配的请求可以是/login或.html或.js或.css方式的请求路径
        但是/不能匹配.jsp请求路径的请求
        -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>
  • Extended configuration

The location and name of the SpringMVC configuration file can be set through the init-param tag, and the initialization time of the SpringMVC front-end controller DispatcherServlet can be set through the load-on-startup tag

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <!--配置SpringMVC的前端控制器,对浏览器发送的请求进行统一处理-->
    <servlet>
        <servlet-name>SpringMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--配置springMVC配置文件的位置和名称-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springMVC.xml</param-value>
        </init-param>
        <!--将前端控制DispatcherServlet的初始化时间提前到服务器启动时-->
        <load-on-startup>1</load-on-startup>
    
    </servlet>
    <servlet-mapping>
        <servlet-name>SpringMVC</servlet-name>
        <!--
        设置springMVC的核心控制器所能处理的请求的请求路径
        /所匹配的请求可以是/login或.html或.js或.css方式的请求路径
        但是/不能匹配.jsp请求路径的请求
        -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

4. Create a request controller

Since the front controller uniformly processes the requests sent by the browser, but specific requests have different processing procedures, it is necessary to create a class that processes specific requests, that is, a request controller.

Every method in the request controller that handles the request becomes a controller method

Because the controller of SpringMVC is served by a POJO (ordinary java class), it needs to be identified as a control layer component through the @Controller annotation and handed over to Spring's IoC container for management. At this time, SpringMVC can recognize the existence of the controller.

5. Create a configuration file for springMVC

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">



    <!--扫描组建-->
    <context:component-scan base-package="org.example"/>

    <!-- 配置Thymeleaf视图解析器 -->
    <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
        <property name="order" value="1"/>
        <property name="characterEncoding" value="UTF-8"/>
        <property name="templateEngine">
            <bean class="org.thymeleaf.spring5.SpringTemplateEngine">
                <property name="templateResolver">
                    <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">

                        <!-- 视图前缀 -->
                        <property name="prefix" value="/WEB-INF/templates/"/>

                        <!-- 视图后缀 -->
                        <property name="suffix" value=".html"/>
                        <property name="templateMode" value="HTML5"/>
                        <property name="characterEncoding" value="UTF-8" />
                    </bean>
                </property>
            </bean>
        </property>
    </bean>
</beans>

6. Test

Create a method in the request controller to handle the request

@RequestMapping("/hello")
public String HelloWorld(){
  return "target";
}

7. Summary

The browser sends a request. If the request address matches the url-pattern of the front-end controller, the request will be processed by the front-end controller DispatchServlet. The front-end controller will read the core configuration file of SpringMVC, find the controller by scanning components, and send the request address Match the value attribute value of the @RequestMapping annotation in the controller. If the match is successful, the controller method identified by the annotation is the method for processing the request. The method of processing the request needs to return a string-type view name, which will be parsed by the view resolver, plus prefixes and suffixes to form the path of the view, rendered by Thymeleaf, and finally forwarded to the page corresponding to the view.

Guess you like

Origin blog.csdn.net/weixin_44516623/article/details/127934613