标签:extends import text util java and 父类 目录 技术分享
Web开发的自动配置类:org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration
视图的配置mvcProperties对象中:
org.springframework.boot.autoconfigure.web.WebMvcProperties.View
如果进入SpringMVC的规则为/时,Spring Boot的默认静态资源的路径为:
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/
测试:
将静态资源放置到webapp下的static目录中即可通过地址访问:
测试:
自定义消息转化器,只需要在@Configuration的类中添加消息转化器的@bean加入到Spring容器,就会被Spring Boot自动加入到容器中。
@Bean public StringHttpMessageConverter stringHttpMessageConverter(){ StringHttpMessageConverter converter = new StringHttpMessageConverter(Charset.forName("UTF-8")); return converter; }
默认配置:
有些时候我们需要自已配置SpringMVC而不是采用默认,比如说增加一个拦截器,这个时候就得通过继承WebMvcConfigurerAdapter然后重写父类中的方法进行扩展。
1 import java.nio.charset.Charset; 2 3 import java.util.List; 4 5 6 7 import javax.servlet.http.HttpServletRequest; 8 9 import javax.servlet.http.HttpServletResponse; 10 11 12 13 import org.springframework.context.annotation.Configuration; 14 15 import org.springframework.http.converter.HttpMessageConverter; 16 17 import org.springframework.http.converter.StringHttpMessageConverter; 18 19 import org.springframework.web.servlet.HandlerInterceptor; 20 21 import org.springframework.web.servlet.ModelAndView; 22 23 import org.springframework.web.servlet.config.annotation.InterceptorRegistry; 24 25 import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 26 27 28 29 @Configuration //申明这是一个配置 30 31 public class MySrpingMVCConfig extends WebMvcConfigurerAdapter{ 32 33 34 35 // 自定义拦截器 36 37 @Override 38 39 public void addInterceptors(InterceptorRegistry registry) { 40 41 HandlerInterceptor handlerInterceptor = new HandlerInterceptor() { 42 43 @Override 44 45 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) 46 47 throws Exception { 48 49 System.out.println("自定义拦截器............"); 50 51 return true; 52 53 } 54 55 56 57 @Override 58 59 public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, 60 61 ModelAndView modelAndView) throws Exception { 62 63 64 65 } 66 67 68 69 @Override 70 71 public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, 72 73 Exception ex) throws Exception { 74 75 } 76 77 }; 78 79 registry.addInterceptor(handlerInterceptor).addPathPatterns("/**"); 80 81 } 82 83 84 85 // 自定义消息转化器的第二种方法 86 87 @Override 88 89 public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { 90 91 StringHttpMessageConverter converter = new StringHttpMessageConverter(Charset.forName("UTF-8")); 92 93 converters.add(converter); 94 95 } 96 97 98 99 }
标签:extends import text util java and 父类 目录 技术分享
原文地址:http://www.cnblogs.com/renshengruozhiruchujian/p/7799499.html