增加自定义视图解析器:
# 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:
# src/main/resources/application.yml
spring:
thymeleaf:
cache: false # 关闭thymeleaf缓存
servlet:
context-path: /home # 设置项目路径
mvc:
date-format: yyyy-MM-dd # 配置日期格式化,默认为dd/MM/yyyy