WebMvcConfigurer 配置类其实是Spring内部的一种配置方式,采用JavaBean的形式来代替传统的xml配置文件形式进行针对框架个性化定制。
基于 java-based 方式的 spring mvc 配置,需要创建一个配置类并实现 WebMvcConfigurer 接口,WebMvcConfigurerAdapter 抽象类是对WebMvcConfigurer接口的简单抽象(增加了一些默认实现),但在在SpringBoot2.0及Spring5.0中WebMvcConfigurerAdapter已被废弃 。官方推荐直接实现 WebMvcConfigurer 或者直接继承 WebMvcConfigurationSupport,方式一实现WebMvcConfigurer接口(推荐),方式二继承WebMvcConfigurationSupport类.
常用的方法:
/* 拦截器配置 */ void addInterceptors(InterceptorRegistry var1); /* 视图跳转控制器 */ void addViewControllers(ViewControllerRegistry registry); /* 静态资源处理 */ void addResourceHandlers(ResourceHandlerRegistry registry); /* 默认静态资源处理器 */ void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer); /* 视图解析器 */ void configureViewResolvers(ViewResolverRegistry registry); /* 配置内容裁决的一些选项*/ void configureContentNegotiation(ContentNegotiationConfigurer configurer); /** 解决跨域问题 **/ public void addCorsMappings(CorsRegistry registry) ;
1 addInterceptors:拦截器
- addInterceptor:需要一个实现HandlerInterceptor接口的拦截器实例
- addPathPatterns:用于设置拦截器的过滤路径规则;
addPathPatterns("/**")
对所有请求都拦截 - excludePathPatterns:用于设置不需要拦截的过滤规则
- 拦截器主要用途:进行用户登录状态的拦截,日志的拦截等。
@Override public void addInterceptors(InterceptorRegistry registry) { super.addInterceptors(registry); registry.addInterceptor(new TestInterceptor()).addPathPatterns("/**").excludePathPatterns("/emp/toLogin","/emp/login","/js/**","/css/**","/images/**"); }
public class MyInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("preHandle,ok,假设给你一个true,运行去吧"); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("postHandle,ok,看看我什么时候运行的。"); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("afterCompletion,ok,看完不点个赞再走吗?"); } }
@Configuration public class MyConfigurer implements WebMvcConfigurer { @Bean public MyInterceptor getMyInterceptor(){ return new MyInterceptor(); } @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(this.getMyInterceptor()) .addPathPatterns("/abc","/configurePathMatch"); } }
2 addViewControllers:页面跳转
以前写SpringMVC的时候,如果需要访问一个页面,必须要写Controller类,然后再写一个方法跳转到页面,感觉好麻烦,其实重写WebMvcConfigurer中的addViewControllers方法即可达到效果了.
@Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/toLogin").setViewName("login"); }
@Override public void addViewControllers(ViewControllerRegistry registry) { super.addViewControllers(registry); // 对 "/hello" 的 请求 redirect 到 "/home" registry.addRedirectViewController("/hello", "/home"); // 对 "/admin/**" 的请求 返回 404 的 http 状态 registry.addStatusController("/admin/**", HttpStatus.NOT_FOUND); // 将 "/home" 的 请求响应为返回 "home" 的视图 registry.addViewController("/home").setViewName("home"); }
3 addResourceHandlers:静态资源
比如,我们想自定义静态资源映射目录的话,只需重写addResourceHandlers方法即可。
@Configuration public class MyWebMvcConfigurerAdapter implements WebMvcConfigurer { /** * 配置静态访问资源 * @param registry */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/my/**").addResourceLocations("classpath:/my/"); } }
- addResoureHandler:指的是对外暴露的访问路径
- addResourceLocations:指的是内部文件放置的目录
@Override public void addResourceHandlers(ResourceHandlerRegistry registry) { super.addResourceHandlers(registry); // 通过 "/home.html" 请求, 来访问 /resource/static/home.html 静态资源 registry.addResourceHandler("/home.html").addResourceLocations("classpath:/static/home.html"); }
4 configureDefaultServletHandling:默认静态资源处理器
@Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable(); configurer.enable("defaultServletName"); }
此时会注册一个默认的Handler:DefaultServletHttpRequestHandler,这个Handler也是用来处理静态文件的,它会尝试映射/。当DispatcherServelt映射/时(/ 和/ 是有区别的),并且没有找到合适的Handler来处理请求时,就会交给DefaultServletHttpRequestHandler 来处理。注意:这里的静态资源是放置在web根目录下,而非WEB-INF 下。
5 configureViewResolvers:视图解析器
这个方法是用来配置视图解析器的,该方法的参数ViewResolverRegistry 是一个注册器,用来注册你想自定义的视图解析器等。
/** * 配置请求视图映射 * @return */ @Bean public InternalResourceViewResolver resourceViewResolver() { InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver(); //请求视图文件的前缀地址 internalResourceViewResolver.setPrefix("/WEB-INF/jsp/"); //请求视图文件的后缀 internalResourceViewResolver.setSuffix(".jsp"); return internalResourceViewResolver; } /** * 视图配置 * @param registry */ @Override public void configureViewResolvers(ViewResolverRegistry registry) { super.configureViewResolvers(registry); registry.viewResolver(resourceViewResolver()); /*registry.jsp("/WEB-INF/jsp/",".jsp");*/ }
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry; //导入依赖的package包/类 @Override public void configureViewResolvers(ViewResolverRegistry registry) { super.configureViewResolvers(registry); InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); viewResolver.setPrefix("/pages/"); viewResolver.setSuffix(".jsp"); registry.viewResolver(viewResolver); }
REF
https://blog.csdn.net/zhangpower1993/article/details/89016503
@Configuration @PropertySource("classpath:ignoreUrl.properties") //等同于<context:component-scan base-package="com.itheima.controller"/> @ComponentScan({"com.itheima.controller"}) /*@Import({MyWebMvcConfig.class})*/ @EnableWebMvc public abstract class SpringMvcConfig implements WebMvcConfigurer { @Value("#{'${ignoreUrl}'.split(',')}") private List<String> ignoreUrl; @Bean public ResourcesInterceptor resourcesInterceptor(){ return new ResourcesInterceptor(ignoreUrl); } /* * 在注册的拦截器类中添加自定义拦截器 * addPathPatterns()方法设置拦截的路径 * excludePathPatterns()方法设置不拦截的路径 */ @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor( resourcesInterceptor()).addPathPatterns("/**").excludePathPatterns("/css/**","/js/**","/img/**"); } /* *开启对静态资源的访问 * 类似在Spring MVC的配置文件中设置<mvc:default-servlet-handler/>元素 */ @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable(); } @Override /* 配置视图解析器的,该方法的参数 ViewResolverRegistry 是一个注册器,用来注册你想自定义的视图解析器等。 */ public void configureViewResolvers(ViewResolverRegistry registry) { registry.jsp("/admin/",".jsp"); /* 前缀为 /admin/ 后缀为 .jsp */ } }
===========================================
@Result(id = true
id=true 对应于主键
@Select("select * from user") @Results({ @Result(id = true,property = "id",column = "id"),//id=true 对应于主键 @Result(property = "uid",column = "uid"), @Result(property = "user",column = "uid",javaType = User.class, one = @One(select = "com.example.dao.UserDao.findUserByid",fetchType = FetchType.DEFAULT)) //user 对应实体类中一对一的实体类名字,uid表示通过uid外键查询User,JavaType表示查询结果 //映射成User类型对象,one=表示一对xx fetchType.default默认是立即加载全部查询,使用lazy懒加载需要才查询 }) List<User> selectUserList();
REF:https://blog.csdn.net/fmwind/article/details/81235401