装配扩展SpringMVC

SpringBoot 文档:Spring Boot Reference Documentation 4.7.1节

添加拦截器,消息转换器,视图解析器等

将视图解析器从spring接管到自己接管

(1)视图解析器

//扩展MVC  添加@Configuration(不能加@EnableWebMvc),实现WebMvcConfigurer
        @Configuration
        public class MyMvcConfiger implements WebMvcConfigurer {
            //注入到springboot
            @Bean
            public ViewResolver myViewResolver(){
                return new com.my.config.MyMvcConfiger.MyViewResolver();
            }
            //自定义视图解析器
            public static class MyViewResolver implements ViewResolver{
                @Override
        public View resolveViewName(String viewName, Locale locale) throws Exception {
            return null;
        }
    }
}

在DispatcherServlet.class中的doDispatch方法上打断点就可以看到自己的ViewResolver

​ 为什么不能加@EnableWebMvc:

@EnableWebMvc -->@Import({DelegatingWebMvcConfiguration.class}) 作用:导入DelegatingWebMvcConfiguration.class  DelegatingWebMvcConfiguration.class extends WebMvcConfigurationSupport    
 而在WebMvcAutoConfiguration.class中 :@ConditionalOnMissingBean({WebMvcConfigurationSupport.class})不能有WebMvcConfigurationSupport.class,否则WebMvcAutoConfiguration.class失效,即所有的自动配置失效

(2)格式化转换器

#配置时间日期格式
spring.gson.date-format=dd/MM/yyyy

(3)视图跳转

@Configuration
public class MyMvcConfiger implements WebMvcConfigurer {
    //视图跳转

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/my").setViewName("/test");
        WebMvcConfigurer.super.addViewControllers(registry);
    }
}

静态资源及路由

所有的静态页面资源html用thymeleaf需要加命名空间;th:href=@{/} 从根目录(classpath)开始

<html lang="en">
<html lang="en" xmlns:th="http://www.thymeleaf.org">

实体类--->dao(绑mybaties用mapper) @Repository--->services @Service--->controller @Controller

posted @   清水煮岁月  阅读(38)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 【杂谈】分布式事务——高大上的无用知识?
点击右上角即可分享
微信分享提示