【学习笔记】SpringMVC

60 阅读2分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第6天,点击查看活动详情

初识SpringMVC

SpringMVC概述

  • SpringMVC是Spring子框架

  • SpringMVC是Spring为 【展现层|表示层|表述层|控制层】 提供的基于MVC设计理念的优秀Web框架,是目前主流的MVC框架。

  • SpringMVC是非侵入式的,可以使用注解,将普通的pojo,作为请求处理器【Controller】。

  • 我们使用SpringMVC代替Servlet的功能

    Servlet作用

    1. 处理请求。

      将数据共享到域中。

    2. 做出相应。

      跳转页面。

SpringMVC处理请求原理

请求发送

  • DispatcherServlet【前端控制器】

    • 将请求交给Controller|Handler
  • Controller|Handler【请求处理器】

    • 处理请求
    • 返回数据模型
  • ModelAndView

    • Model:数据模型
    • View:视图对象或视图名
  • DispatcherServlet渲染视图

    • 将数据共享到域中
    • 跳转页面【视图】
  • 响应

SpringMVC框架搭建

搭建SpringMVC框架

  • 创建Web工程【详见之前的笔记】

  • 导入jar包

    <!--spring-webmvc-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.3.1</version>
    </dependency><!-- 导入thymeleaf与spring5的整合包 -->
    <dependency>
        <groupId>org.thymeleaf</groupId>
        <artifactId>thymeleaf-spring5</artifactId>
        <version>3.0.12.RELEASE</version>
    </dependency><!--servlet-api-->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>4.0.1</version>
        <scope>provided</scope>
    </dependency>
    
  • 编写配置文件

    • web.xml注册DispatcherServlet

      • url配置:/
      • init-param:contextConfigLocation,设置springmvc.xml配置文件路径【管理容器对象】
      • :设置DispatcherServlet优先级【启动服务器时,创建当前Servlet对象】

      示例代码:

      <?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">
          <!--    注册DispatcherServlet【前端控制器】-->
          <servlet>
              <servlet-name>DispatcherServlet</servlet-name>
              <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          <!--        设置springmvc.xml配置文件路径【管理容器对象】-->
              <init-param>
                  <param-name>contextConfigLocation</param-name>
                  <param-value>classpath:springmvc.xml</param-value>
              </init-param>
          <!--   设置DispatcherServlet优先级【在启动服务器时,就创建Servlet对象】     -->
              <load-on-startup>1</load-on-startup>
          </servlet>
          <servlet-mapping>
              <servlet-name>DispatcherServlet</servlet-name>
              <url-pattern>/</url-pattern>
          </servlet-mapping></web-app>
      
    • springmvc.xml

      • 开启组件扫描
      • 配置视图解析器【解析视图(设置视图前缀&后缀)】

      示例代码:

      <?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="SpringMVC"></context:component-scan>
      <!--    - 配置视图解析器【解析视图(设置视图前缀&后缀)】-->
          <bean class="org.thymeleaf.spring5.view.ThymeleafViewResolver" id="viewResolver">
      <!--       配置字符集属性 -->
              <property name="characterEncoding" value="UTF-8"></property>
      <!--        配置模板引擎属性-->
              <property name="templateEngine">
      <!--            配置内部bean-->
                  <bean class="org.thymeleaf.spring5.SpringTemplateEngine">
      <!--                配置模块的解析器属性-->
                      <property name="templateResolver">
      <!--                    配置内部bean-->
                          <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
      <!--                        配置前缀-->
                              <property name="prefix" value="/WEB-INF/pages/"></property>
      <!--                        配置后缀-->
                              <property name="suffix" value=".html"></property>
      <!--                        配置字符集-->
                              <property name="characterEncoding" value="UTF-8"></property>
                          </bean>
                      </property>
      ​
                  </bean>
              </property>
      ​
          </bean>
      </beans>
      
  • 编写请求处理器【Controller|Handler】

    • 使用 @Controller注解标识请求处理器
    • 使用 @RequestMapping注解标识处理方法【URL】

    示例代码:

    @Controller  //当前类是请求处理器类。
    public class HelloController {
    ​
        /*
        配置url【/】,就映射到WEB-INF/index.html
         */
        @RequestMapping("/")
        public String toIndex(){
            //逻辑视图名映射到物理视图名
            // WEB-INF/pages index      .html
             //视图前缀     +  逻辑视图名 + 视图后缀
            return "index";
        }
        @RequestMapping("/HelloController")
        public String HelloWorld(){
            System.out.println("HelloWorld in HelloController");
            return "success";
        }
    }
    
  • 准备页面进行,测试

    <!DOCTYPE html>
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>Hello</title>
    </head>
    <body>
    <a th:href="@{/HelloController}">发送请求</a>
    </body>
    </html>
    

    \