Web开发的自动配置类:org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration

1.1. 自动配置的ViewResolver

视图的配置mvcProperties对象中:

org.springframework.boot.autoconfigure.web.WebMvcProperties.View

1.2. 自动配置静态资源

1.2.1. 进入规则为 /

如果进入SpringMVC的规则为/时,Spring Boot的默认静态资源的路径为:

spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/

测试:

1.2.2. 进入规则为*.xxx 或者 不指定静态文件路径时

将静态资源放置到webapp下的static目录中即可通过地址访问:

测试:

1.3. 自定义消息转化器

自定义消息转化器,只需要在@Configuration的类中添加消息转化器的@bean加入到Spring容器,就会被Spring Boot自动加入到容器中。

    @Bean

    public StringHttpMessageConverter stringHttpMessageConverter(){

        StringHttpMessageConverter converter  = new StringHttpMessageConverter(Charset.forName("UTF-8"));

        return converter;

}

默认配置:

1.4. 自定义SpringMVC的配置

有些时候我们需要自已配置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 }

优质内容筛选与推荐>>
1、Andriod线性布局LinearLayout
2、lseek()函数
3、日出时分的云彩 (05/10/2010)
4、JQUERY mouseover事件指针穿过子元素同样会触发 .
5、三种算法求解一个数组的子数组最大和


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号





    联系我们

    欢迎来到TinyMind。

    关于TinyMind的内容或商务合作、网站建议,举报不良信息等均可联系我们。

    TinyMind客服邮箱:support@tinymind.net.cn