Spring內部的一種配置方式
採用JavaBean的形式來代替傳統的xml配置文件形式進行鍼對框架個性化定製html
/** 解決跨域問題 **/ public void addCorsMappings(CorsRegistry registry) ; /** 添加攔截器 **/ void addInterceptors(InterceptorRegistry registry); /** 這裏配置視圖解析器 **/ void configureViewResolvers(ViewResolverRegistry registry); /** 配置內容裁決的一些選項 **/ void configureContentNegotiation(ContentNegotiationConfigurer configurer); /** 視圖跳轉控制器 **/ void addViewControllers(ViewControllerRegistry registry); /** 靜態資源處理 **/ void addResourceHandlers(ResourceHandlerRegistry registry); /** 默認靜態資源處理器 **/ void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer);
excludePathPatterns:用於設置不須要攔截的過濾規則java
@Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**").excludePathPatterns("/toLogin","/login"); super.addInterceptors(registry); }
@Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**").allowedOrigins("*").allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE").maxAge(3600).allowCredentials(true); }
/** * 之前要訪問一個頁面須要先建立個Controller控制類,在寫方法跳轉到頁面 * 在這裏配置後就不須要那麼麻煩了,直接訪問http://localhost:8080/toLogin就跳轉到login.html頁面了 * * @param registry */ @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/toLogin").setViewName("login"); registry.addViewController("/").setViewName("/index"); registry.addViewController("/login").setViewName("forward:/index.html"); super.addViewControllers(registry); }
/** * 配置請求視圖映射 * @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");*/ }
/** * 消息內容轉換配置 * 配置fastJson返回json轉換 * @param converters */ @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { //調用父類的配置 super.configureMessageConverters(converters); //建立fastJson消息轉換器 FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); //建立配置類 FastJsonConfig fastJsonConfig = new FastJsonConfig(); //修改配置返回內容的過濾 fastJsonConfig.setSerializerFeatures( SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullStringAsEmpty ); fastConverter.setFastJsonConfig(fastJsonConfig); //將fastjson添加到視圖消息轉換器列表內 converters.add(fastConverter); }
@Override public void addResourceHandlers(ResourceHandlerRegistry registry) { //自定義項目內目錄 //registry.addResourceHandler("/my/**").addResourceLocations("classpath:/my/"); //指向外部目錄 registry.addResourceHandler("/my/**").addResourceLocations("file:E:/my/"); super.addResourceHandlers(registry); }
該方法在spring boot 2.0,Spring 5.0 以後,已經被廢棄 spring
@Configuration public class WebConfig extends WebMvcConfigurerAdapter { //TODO }
①直接實現WebMvcConfigurerjson
@Configuration public class WebMvcConfg implements WebMvcConfigurer { //TODO }
②直接繼承WebMvcConfigurationSupport跨域
@Configuration public class WebMvcConfg extends WebMvcConfigurationSupport { //TODO }
查看源碼發現: WebMvcConfigurerAdapter只是對WebMvcCofigurer的空實現app
WebMvcConfigurationSupport與WebMvcConfigurerAdapter、接口WebMvcConfigurer處於同一個目錄下框架
WebMvcConfigurationSupport包含WebMvcConfigurer裏面的方法,且WebMvcConfigurationSupport的實現的方法更全面jsp
可是繼承WebMvcConfigurationSupport會發現Spring Boot的WebMvc自動配置失效(WebMvcAutoConfiguration自動化配置),致使沒法視圖解析器沒法解析並返回到對應的視圖。ide
關於使用新方案會出現的問題,能夠參考下面這篇文章。spa