SpringBoot 之 扩展 SpringMVC

使用自定义视图解析器:

# src/main/java/com/wu/config/MyMvcConfig.java

@Configuration // 标注这个类是一个配置类
public class MyMvcConfig implements WebMvcConfigurer {
    // 将视图解析器配置到容器中
    @Bean
    public ViewResolver myViewResolver() {
        return new MyViewResolver();
    }
    
    // 自定义一个视图解析器
    public static class MyViewResolver implements ViewResolver {
        @Override
        public View resolveViewName(String viewName, Locale locale) throws Exception {
            return null;
        }
    }
}

配置自定义视图跳转:

# src/main/java/com/wu/config/MyMvcConfig.java

@Configuration // 标注这个类是一个配置类
public class MyMvcConfig implements WebMvcConfigurer {
    // 配置视图跳转
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index");
        registry.addViewController("/index.html").setViewName("index");
    }
}

通过配置扩展 SpringMVC:

# 关闭thymeleaf缓存
spring.thymeleaf.cache=false
# 设置项目路径
spring.servlet.context-path=/home
# 配置日期格式化,默认为dd/MM/yyyy
spring.mvc.date-format=yyyy-MM-dd
点赞