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的自动配置共同起作用

  • 相关阅读:
    Azure PowerShell (7) 使用CSV文件批量设置Virtual Machine Endpoint
    Windows Azure Cloud Service (39) 如何将现有Web应用迁移到Azure PaaS平台
    Azure China (7) 使用WebMetrix将Web Site发布至Azure China
    Microsoft Azure News(4) Azure新D系列虚拟机上线
    Windows Azure Cloud Service (38) 微软IaaS与PaaS比较
    Windows Azure Cloud Service (37) 浅谈Cloud Service
    Azure PowerShell (6) 设置单个Virtual Machine Endpoint
    Azure PowerShell (5) 使用Azure PowerShell创建简单的Azure虚拟机和Linux虚拟机
    功能代码(1)---通过Jquery来处理复选框
    案例1.用Ajax实现用户名的校验
  • 原文地址:https://www.cnblogs.com/vegeta-xiao/p/12509010.html
Copyright © 2011-2022 走看看