自己编写配置类并实现WebMvcConfigurer接口
/** * 使用WebMvcConfigurer可以来扩展SpringMVC的功能 * @EnableWebMvc 全面接管SpringMVC,这个注解一般不用 */ @Configuration public class MyMvcConfig implements WebMvcConfigurer { /** * 添加视图解析器 */ @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/lalala").setViewName("success"); registry.addViewController("/").setViewName("login"); registry.addViewController("/index.html").setViewName("login"); registry.addViewController("/main.html").setViewName("dashboard"); } /** * 注册拦截器 */ @Override public void addInterceptors(InterceptorRegistry registry) { //SpringBoot已经做好了静态资源映射 registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**") .excludePathPatterns("/index.html","/","/user/login"); } /** * 自定义国际化化区域解析器 */ @Bean public LocaleResolver localeResolver(){ return new MyLocaleResolver(); } }
/** * 自定义拦截器,登陆检查 */ public class LoginHandlerInterceptor implements HandlerInterceptor { //目标方法执行之前 @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { Object user = request.getSession().getAttribute("loginUser"); if(user == null){ //未登陆,返回登陆页面 request.setAttribute("msg","没有权限请先登陆"); request.getRequestDispatcher("/index.html").forward(request,response); return false; }else{ //已登陆,放行请求 return true; } } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { } }
/** * 自定义区域解析器,可以在连接上携带区域信息 */ public class MyLocaleResolver implements LocaleResolver { @Override public Locale resolveLocale(HttpServletRequest request) { String l = request.getParameter("l"); Locale locale = Locale.getDefault(); if(!StringUtils.isEmpty(l)){ String[] split = l.split("_"); locale = new Locale(split[0],split[1]); } return locale; } @Override public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) { } }
这种配置方式会和springboot的自动配置同时起作用,推荐使用,而之所以会同时起作用的原理如下WebMvcAutoConfiguration中有一个静态内部类EnableWebMvcConfiguration
@Configuration(proxyBeanMethods = false) public static class EnableWebMvcConfiguration extends DelegatingWebMvcConfiguration implements ResourceLoaderAware { }
在EnableWebMvcConfiguration的继承的DelegatingWebMvcConfiguration类中
@Configuration( proxyBeanMethods = false ) public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport { private final WebMvcConfigurerComposite configurers = new WebMvcConfigurerComposite(); public DelegatingWebMvcConfiguration() { } @Autowired( required = false ) // 可以看到这里是将所有实现的WebConfigurer加入配置中 public void setConfigurers(List<WebMvcConfigurer> configurers) { if (!CollectionUtils.isEmpty(configurers)) { this.configurers.addWebMvcConfigurers(configurers); } } }
所以我们自定义一个类实现WebMvcConfigurer,并加入配置中就会自动生效,并且是和springboot的自动配置共同起作用