zoukankan      html  css  js  c++  java
  • springboot中springmvc的自定义配置

    自己编写配置类并实现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的自动配置共同起作用

  • 相关阅读:
    《软件测试经验与教训》—读书笔记
    【转】性能测试工程师的素质
    【转】如何成为优秀的性能测试工程师
    性能测试学习之路
    FTP 、TCP/IP、HTTP、Cookies、Session
    Loadrunner工具介绍
    tesseract-ocr图像识别技术(一)
    MongoDB 自动分片 auto sharding
    mongodb 3.0下载安装、配置及mongodb最新特性、基本命令教程详细介绍
    java使用memcached2--集群部署
  • 原文地址:https://www.cnblogs.com/vegeta-xiao/p/12509010.html
Copyright © 2011-2022 走看看