2018-03-08
springboot-集成springmvc的配置
springboot 评论:0 浏览:122

转载请注明出处:https://oldnoop.tech/c/160.html

静态资源文件版本号支持

修改application.properties

#开启静态资源动态版本号支持,避免浏览器缓存静态文件(js/css/图片等),便于开发调试
spring.resources.chain.strategy.content.enabled=true
spring.resources.chain.strategy.content.paths=/**

设置默认首页

springboot推荐编码式配置,web工程中没有web.xml文件非常正常

web.xml中会定义默认首页,在springboot中通过ViewControllerRegistry设置

日期转换

服务端接收请求数据的日期类型转换

编写一个日期转换类,将请求数据中的字符串转换为日期类型

服务端返回json数据中的日期类型转换

设置jackson的日期格式化

修改application.properties

#配置jackson
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.locale=cn
spring.jackson.time-zone=GMT+8

如果不够用,

可以在实体类对象的属性上添加jackson的注解

@JsonFormat(pattern="yyyy-MM-dd",locale="cn",timezone="GMT+8")

还可以在前端视图页面使用js的日期插件再次转换,

国际化

基于cookie实现的国际化,配置国际化消息,国际化解析器,国际化拦截器

代码如下:

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {

    //设置默认首页
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("redirect:/index");
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
        super.addViewControllers(registry);
    }
    
    //日期装换类
    @Bean
    public DateConvertor dateConvertor(){
        return new DateConvertor();
    }
    
    // 国际化区域解析器
    @Bean(name = "localeResolver")
    public CookieLocaleResolver cookieLocaleResovler() {
        CookieLocaleResolver cookieLocaleResolver = new CookieLocaleResolver();
        cookieLocaleResolver.setCookieName("locale");
        cookieLocaleResolver.setCookieMaxAge(31536000);
        cookieLocaleResolver.setDefaultLocale(Locale.SIMPLIFIED_CHINESE);
        return cookieLocaleResolver;
    }
    
    //国际化操作拦截器
    @Bean
    public LocaleChangeInterceptor localeChangeInterceptor() {
        LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
        // 参数名
        lci.setParamName("locale");
        return lci;
    }
    
    //国际化信息资源文件
    @Bean
    public ResourceBundleMessageSource resource(){
        ResourceBundleMessageSource resource = new ResourceBundleMessageSource();
        resource.setBasename("message");
        resource.setUseCodeAsDefaultMessage(true);
        return resource;
    }
    
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(localeChangeInterceptor());
        //如果需要添加其他拦截器,比如登录验证拦截器
        //registry.addInterceptor(new SecurityInterceptor())
        //        .addPathPatterns("/**")
        //        .excludePathPatterns(
        //            "/static","/user/login",
        //            "/user/doLogin","/user/logout",
        //            "/user/verifycode");
    }   

    static class DateConvertor implements Converter<String, Date> {
        public static final String YYYY_MM_DD = "yyyy-MM-dd";
        public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

        @Override
        public Date convert(String text) {
            if (text == null) {
                return null;
            }
            text = text.trim();
            try {
                if (YYYY_MM_DD.length() == text.length()) {
                    return new SimpleDateFormat(YYYY_MM_DD).parse(text);
                }

                if (YYYY_MM_DD_HH_MM_SS.length() == text.length()) {
                    return new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS).parse(text);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    }
}

 



  • 转载请注明出处:https://oldnoop.tech/c/160.html

Copyright © 2018 oldnoop.tech. All Rights Reserved

鄂ICP备2023022735号-1