MVC配置原理

如果你想保存springboot的mvc配置并且还想自己添加自己的配置就用这个。

视图解析器原理,它会从IOC容器里获取配置好视图解析器的配置类里的视图解析器集合,

然后遍历集合,生成一个一个的视图对象,放入候选 视图里,然后返回这个候选视图。

DispatcherServlet 所有的请求都会走 diDispatch() 方法

java 复制代码
package com.kuang.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Locale;

//扩展WebMvc  所有请求会经过dispatcherServlet
//1.这是一个配置类
@Configuration
//2.实现WebMvcConfigurer这个接口
//记住不要标注它,@EnableWebMvc,一旦标注代表MVC全面被你接管,springboot自动配置不生效,很多东西系统配置好了,全面接管就是重新写
public class MyMvcConfig implements WebMvcConfigurer {

    //ViewResolver 实现了视图解析器接口的类,我们就可以把它看作视图解析器

    //把自定义视图解析器放入IOC容器里调用
    @Bean
    public ViewResolver viewResolver(){
        return new MyViewResolver();
    }


    //自定义了一个自己的视图解析器ViewResolver
    public static class MyViewResolver implements ViewResolver{
        @Override
        public View resolveViewName(String viewName, Locale locale) throws Exception {
            return null;
        }
    }




}

只要实现ViewResolver接口,然后把这个对象,放入IOC容器里,DispatcherServlet就会自动扫描并且装配上去,//如果。你想diy一些定制化的功能,只要写这个组件,然后将它交给springboot,springboot就会帮我们自动装配!

java 复制代码
@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 这种方式也会覆盖掉默认的web静态资源目录
        registry.addResourceHandler("/**").addResourceLocations("classpath:static/","classpath:templates/");
    }
java 复制代码
package com.kuang.config;

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

import java.util.Locale;

//扩展WebMvc  所有请求会经过dispatcherServlet
//1.这是一个配置类
@Configuration
//2.实现WebMvcConfigurer这个接口
//记住不要标注它,@EnableWebMvc,一旦标注代表MVC全面被你接管,springboot自动配置不生效,很多东西系统配置好了,全面接管就是重新写
public class MyMvcConfig implements WebMvcConfigurer {

//    //ViewResolver 实现了视图解析器接口的类,我们就可以把它看作视图解析器
//
//    //把自定义视图解析器放入IOC容器里调用
//    @Bean
//    public ViewResolver viewResolver(){
//        return new MyViewResolver();
//    }
//
//
//    //自定义了一个自己的视图解析器ViewResolver
//    public static class MyViewResolver implements ViewResolver{
//        @Override
//        public View resolveViewName(String viewName, Locale locale) throws Exception {
//            return null;
//        }
//    }


//    视图跳转
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/kuang").setViewName("test");
    }
}

视图跳转,通过转发又走Thymeleaf模板的视图解析器,转发到 /template/test.html 来拼接 网页进入到这个网页

结论:如果我们要扩展一个配置,官方建议我们 去实现一个XXXConfigurer 接口,来自己配置部分设置,剩下不配的交给springboot来自动配置。

复制代码
@EnableWebMvc

导入了一个类:

复制代码
DelegatingWebMvcConfiguration.class 这个类继承了它,所以相当于使全部配置失效
相关推荐
小小马喽_Thendras2 分钟前
ScheduledExecutorService 和Timer的区别
java·开发语言
小江的记录本10 分钟前
【Swagger】Swagger系统性知识体系全方位结构化总结
java·前端·后端·python·mysql·spring·docker
空太Jun11 分钟前
Spring Security 自定义数据库认证(初尝试)
java·数据库·spring
sinat_2554878118 分钟前
泛型·学习笔记
java·jvm·数据库·windows·python
QuZero23 分钟前
Java Synchronized principle
java·开发语言
明灯伴古佛26 分钟前
面试:Java中乐观锁的实现原理是什么
java·面试·职场和发展
SimonKing31 分钟前
白嫖党狂喜!魔塔社区每天2000次免费大模型调用,真香!
java·后端·程序员
lifallen1 小时前
Flink Agent 与 Checkpoint:主循环闭环与 Mailbox 事件驱动模型
java·大数据·人工智能·python·语言模型·flink
小则又沐风a1 小时前
类和对象----最终篇
java·前端·数据库
喵叔哟1 小时前
4.【.NET10 实战--孢子记账--产品智能化】--C# 14 新语法特性详解与实战应用
java·c#·.net