装配扩展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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 【杂谈】分布式事务——高大上的无用知识?