【web开发】扩展与全面接管springmvc

2023-10-27

1. 扩展springmvc

方法:编写一个配置类(@Configuration),实现WebMvcConfigurer接口,不能标注@EnableWebMvc。
特点:既保留了springboot所有的自动配置,也能用我们扩展的配置。
示例:
pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.qublog</groupId>
    <artifactId>springboot03_web_restfulcrud</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot03_web_restfulcrud</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- 引入thymeleaf -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <!-- 引入jquery的webjar -->
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>jquery</artifactId>
            <version>3.5.1</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Springboot03WebRestfulcrudApplication类:

package com.qublog.springboot03_web_restfulcrud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;

import java.util.Locale;

@SpringBootApplication
public class Springboot03WebRestfulcrudApplication {

    public static void main(String[] args) {
        SpringApplication.run(Springboot03WebRestfulcrudApplication.class, args);
    }

    @Bean
    public ViewResolver myViewResolver() {
        return new MyViewResolver();
    }

    private static class MyViewResolver implements ViewResolver {
        @Override
        public View resolveViewName(String s, Locale locale) throws Exception {
            return null;
        }
    }

}

MyMvcConfig类:

package com.qublog.springboot03_web_restfulcrud.config;


import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

//使用WebMvcConfigurer可以来扩展springmvc功能
//由于接口有默认实现,所以可以选择的实现部分方法
@Configuration
public class MyMvcConfig implements WebMvcConfigurer  {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //功能:浏览器发送/qublog请求也来到success页面
        registry.addViewController("/qublog").setViewName("success");
    }
}

success.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h2>success</h2>
    <!-- th:text 将div里面的文本内容指定为后台数据 -->
    <div id="div01" class="myDiv" th:id="${hello}" th:class="${hello}" th:text="${hello}">
        123456
    </div>
    <hr/>
    <div th:text="${hello}"></div>
    <div th:utext="${hello}"></div>
    <hr/>
    <!-- th:each每次遍历都会生成当前这个标签 -->
    <h4 th:text="${user}" th:each="user:${users}"></h4>
    <hr/>
    <h4>
        <span th:each="user:${users}">[[${user}]]</span>
    </h4>

</body>
</html>

原理

  1. WebMvcAutoConfiguration是springmvc的自动配置类。
  2. 在做其他自动配置类时会导入:EnableWebMvcConfiguration。
	//WebMvcAutoConfiguration类中   
	@Import({WebMvcAutoConfiguration.EnableWebMvcConfiguration.class})
    @EnableConfigurationProperties({WebMvcProperties.class, ResourceProperties.class})
    @Order(0)
    public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer
	
	//WebMvcAutoConfiguration类中
    @Configuration(
        proxyBeanMethods = false
    )
    public static class EnableWebMvcConfiguration extends DelegatingWebMvcConfiguration implements ResourceLoaderAware
  	
  	//DelegatingWebMvcConfiguration类中
  	public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {
    private final WebMvcConfigurerComposite configurers = new WebMvcConfigurerComposite();

    public DelegatingWebMvcConfiguration() {
    }

	//从容器中获取所有的WebMvcConfigurer
    @Autowired(
        required = false
    )
    public void setConfigurers(List<WebMvcConfigurer> configurers) {
        if (!CollectionUtils.isEmpty(configurers)) {
            this.configurers.addWebMvcConfigurers(configurers);
        }

    }
	
	//DelegatingWebMvcConfiguration类中
	protected void addViewControllers(ViewControllerRegistry registry) {
        this.configurers.addViewControllers(registry);
    }

	//一个参考实现,作用:将所有的WebMvcConfigurer相关配置都来一起调用
    public void addViewControllers(ViewControllerRegistry registry) {
        Iterator var2 = this.delegates.iterator();

        while(var2.hasNext()) {
            WebMvcConfigurer delegate = (WebMvcConfigurer)var2.next();
            delegate.addViewControllers(registry);
        }

    }
  1. 容器中所有的WebMvcConfigurer都会一起起作用。
  2. 我们的配置类(MyMvcConfig)也会被调用。

效果:springmvc的自动配置和我们的扩展配置都会起作用。

2. 全面接管springmvc

springboot对springmvc的自动配置不需要了,所有都是我们自己配。只需要在配置类中添加@EnableWebMvc即可。
示例:

package com.qublog.springboot03_web_restfulcrud.config;


import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

//使用WebMvcConfigurer可以来扩展springmvc功能
//由于接口有默认实现,所以可以选择的实现部分方法
@EnableWebMvc
@Configuration
public class MyMvcConfig implements WebMvcConfigurer  {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //功能:浏览器发送/qublog请求也来到success页面
        registry.addViewController("/qublog").setViewName("success");
    }
}

原理

//@EnableWebMvc
@Import({DelegatingWebMvcConfiguration.class})
public @interface EnableWebMvc {
}

//DelegatingWebMvcConfiguration类中
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport

//WebMvcAutoConfiguration类中
@Configuration(
    proxyBeanMethods = false
)
@ConditionalOnWebApplication(
    type = Type.SERVLET
)
@ConditionalOnClass({Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class})
//容器中没有这个组件的时候,这个自动配置类才生效
@ConditionalOnMissingBean({WebMvcConfigurationSupport.class})
@AutoConfigureOrder(-2147483638)
@AutoConfigureAfter({DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class, ValidationAutoConfiguration.class})
public class WebMvcAutoConfiguration

@EnableWebMvc将WebMvcConfigurationSupport组件导入进来,导入的WebMvcConfigurationSupport只是springmvc最基本的功能。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

【web开发】扩展与全面接管springmvc 的相关文章

  • 【超级无敌详细的黑马前端笔记!即时更新~】

    超级无敌详细的黑马前端笔记 即时更新 这个笔记 是我自己在同步学习黑马前端使用的 不可以商用哦 学习路径 基础概念铺垫 了解 认识网页 五大浏览器和渲染引擎 Web标准 HTML初体验 HTML的感知 HTML骨架结构 开发工具的使用 语法
  • 【08】STM32·HAL库开发-HAL库介绍

    目录 1 初识HAL库 了解 1 1CMSIS简介 1 2HAL库简介 2 STM32Cube固件包浅析 了解 2 1如何获取STM32Cube固件包 2 2STM32Cube固件包文件夹简介 2 3CMSIS文件夹关键文件 2 3 1CM
  • 增量式pid分析 及 参数整定

    函数功能 增量PI控制器 1 入口参数 编码器测量值 目标速度 2 入口参数 编码器位置 目标位置 返回 值 电机PWM 根据增量式离散PID公式 pwm Kp e k e k 1 Ki e k Kd e k 2e k 1 e k 2 e

随机推荐

  • 宝塔Linux面板—请使用正确的入口登录面板

    请使用正确的入口登录面板 错误原因 当前新安装的已经开启了安全入口登录 新装机器都会随机一个8位字符的安全入口名称 亦可以在面板设置处修改 如您没记录或不记得了 可以使用以下方式解决 解决方法 1 在SSH终端输入以下一种命令来解决 登录你
  • C# 中 KeyPress 、KeyDown 和KeyPress的区别

    1 KeyPress主要用来捕获数字 注意 包括Shift 数字的符号 字母 注意 包括大小写 小键盘等除了F1 12 SHIFT Alt Ctrl Insert Home PgUp Delete End PgDn ScrollLock P
  • 将时间写入数据库时,秒带小数点问题

    1 问题原因 new Date 时间秒带小数点 2 解决思路 将new Date 格式化为年月日时分秒字符串 在将该字符串转为Date 3 示例 Constant类 public final static SimpleDateFormat
  • VScode中文注释乱码问题解决

    VScode默认是用utf 8打开工程代码 C语言里的中文注释如果是Source insight之前gbk编码的注释可能会显示乱码 如何能让代码打开gbk编码的文件也不乱码 设置VScode如下 方法一 依次打开 文件 首选项 设置 然后搜
  • 电机转角控制算法

    时间周期是1 10ms可调 橙色和绿色代表期望和真实速度 红色代表期望角度 紫色代表电机转角 蓝色是减速机输出角度 在9700时刻角度已到达期望角度 速度由 200还未到0 所以算法上需要在要到达目标前某值就需要提前减速 这个值和当前真实速
  • MySQL查询进阶

    多表查询 ALLSOME查询 聚集函数查询 分组查询 分组过滤查询 Student表格 Dept 院系 学院号 学院名 院长 Course表格 Teacher表格 教师号 教师姓名 部门号 工资 SC表格 学号 课程号 分数 反应学生和课程
  • 使用线程池创建线程

    使用线程池可以不用线程的时候放回线程池 用的时候再从线程池取 1 5后引入的Executor框架的最大优点是把任务的提交和执行解耦 Executor接口中定义了一个方法execute 该方法接收一个Runable实例 它用来执行一个任务 任
  • IDEA插件系列(10):Statistic插件——统计代码行数

    1 插件介绍 显示项目统计信息 此插件显示按扩展名排序的文件 以及大小 行数LOC等 2 安装方式 第一种安装方式是使用IDEA下载插件进行安装 第二种方式是使用离线插件进行安装 插件下载地址 http plugins jetbrains
  • W. Richard Stevens Unix网络编程 作品集

    W Richard Stevens 写的那三卷书以怎样的顺序看是比较好的 按我的经验来看先粗略的看一遍卷一 然后再结合卷一看卷二 卷二要看几遍 每遍都会有收获 然后卷三大概看看就行 之后就是看源码 看bsd lite的 在最后就是看linu
  • svn客户端Tortoise SVN使用方法

    svn客户端Tortoise SVN使用方法 转载自 http www cnblogs com armyfai p 3985660 html 该博主介绍比较详细 这里只转载其中svn客户端使用部分 1 首先我们需要下载 svn小乌龟 后 进
  • Webservice实践(二)Webservice 客户端开发

    现在我们首先进行客户端开发的实践 从客户端实践来了解一下webservice的应用场景 比如说现在已经有一个webservice服务 提供的翻译方面的功能服务 主要是免费的webservice接口现在很多都被封了 我们需要编写一个客户端来使
  • Caffe中计算图像均值的实现(cifar10)

    在深度学习中 在进行test时经常会减去train数据集的图像均值 这样做的好处是 属于数据预处理中的数据归一化 降低数据间相似性 可以将数值调整到一个合理的范围 以下code是用于计算cifar10中训练集的图像均值 include fu
  • 代码越写越乱?那是因为你没用责任链

    路人 Java充电社 2022 08 04 08 06 发表于上海 收录于合集 java充电社244个 大家好 我是路人 最近 我让团队内一位成员写了一个导入功能 他使用了责任链模式 代码堆的非常多 bug 也多 没有达到我预期的效果 实际
  • springboot-单点登录

    单点登录 第一节 简介 1 1 什么是单点登陆 单点登录 Single Sign On 简称为 SSO 是目前比较流行的企业业务整合的解决方案之一 SSO的定义是在多个应用系统中 用户只需要登录一次就可以访问所有相互信任的应用系统 较大的企
  • 2023年美赛C题思路复盘

    论文标题 Riddle of Wordle Mining the Secret of Number Scores Solution Words Wordle之谜 挖掘数字得分和解字词的秘密 文章目录 前言 一 题目重述 拟解决的问题 我们的
  • mybatis异常:Could not find result map XXXX

    在使用mybatis框架 报这个错误时 是mapper文件中 查询语句的返回类型写错了 即该用resultType用成了resultMap 如果你要返回基本类型 或者返回已存在的pojo对象 用resultType修饰 如果要使用resul
  • Android ImageLoader用法总结

    前言 imageloader作为一个开源的框架被广泛的使用 尤其对于新手而言 更是如此 本人在项目中每当用到imageloader的时候都是从网上百度然后复制粘贴 现在觉得那样没得意义 因此写此博客 当作以后发复习资料 同时稍微研究下ima
  • springboot依赖

  • 三层架构:界面层、业务逻辑层、数据访问层

    进行用户的插入操作 1 spring会接管三层架构中的那些对象的创建 2 非spring接管下的三层项目构建 从下往上 1 实体类 com bjpowernode pojo Users package com bjpowernode poj
  • 【web开发】扩展与全面接管springmvc

    1 扩展springmvc 方法 编写一个配置类 Configuration 实现WebMvcConfigurer接口 不能标注 EnableWebMvc 特点 既保留了springboot所有的自动配置 也能用我们扩展的配置 示例 pom