首页 > 编程语言 > 详细

Spring Boot的web开发

时间:2019-04-04 14:52:08      阅读:143      评论:0      收藏:0      [点我收藏+]

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

自动配置的ViewResolver

技术分享图片

视图的配置mvcProperties对象中:

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

技术分享图片


 

自动配置静态资源

1.   进入规则为 /

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

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

但是也可以通过spring.resources.static-locations来指定某些固定路径。

测试:

技术分享图片

技术分享图片

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

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

技术分享图片


 

 

 

自定义消息转化器

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

   @Bean
    public StringHttpMessageConverter stringHttpMessageConverter(){
        StringHttpMessageConverter converter  = new StringHttpMessageConverter(Charset.forName("UTF-8"));
        return converter;
}

但是不配置UTF-8的消息转化器也没关系,因为springboot有一个Jackson的默认配置,所以你写中文也不会乱码。而springmvc的内置转化器默认的是ISO-8859-1,会乱码。

默认配置:

技术分享图片

 

 技术分享图片


自定义SpringMVC的配置

有些时候我们需要自已配置SpringMVC而不是采用默认,比如说增加一个拦截器,这个时候就得通过继承WebMvcConfigurerAdapter然后重写父类中的方法进行扩展。

import java.nio.charset.Charset;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration //申明这是一个配置
public class MySrpingMVCConfig extends WebMvcConfigurerAdapter{

    // 自定义拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        HandlerInterceptor handlerInterceptor = new HandlerInterceptor() {
            @Override
            public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
                    throws Exception {
                System.out.println("自定义拦截器............");
                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 {
            }
        };
        registry.addInterceptor(handlerInterceptor).addPathPatterns("/**");
    }

    // 自定义消息转化器的第二种方法
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        StringHttpMessageConverter converter  = new StringHttpMessageConverter(Charset.forName("UTF-8"));
        converters.add(converter);
    }

}

 

Spring Boot的web开发

原文:https://www.cnblogs.com/xk920/p/10654749.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!