MVC配置原理

 

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

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

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

 

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

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就会帮我们自动装配!

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

 

 

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 这个类继承了它,所以相当于使全部配置失效

 

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值