package com.nhfc99.template.Config; import org.springframework.core.convert.converter.Converter; import org.springframework.lang.Nullable; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class StringToDateConverter implements Converter<String, Date> { @Nullable @Override public Date convert(String json) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = null; try { date = sdf.parse((String) json); } catch (Exception e) { SimpleDateFormat sdfday = new SimpleDateFormat("yyyy-MM-dd"); try { date = sdfday.parse((String) json); } catch (ParseException e1) { e1.printStackTrace(); } } return date; } } package com.nhfc99.template.Config; import org.springframework.context.annotation.Configuration; import org.springframework.core.annotation.Order; import org.springframework.format.FormatterRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; @Order(1) @Configuration public class ApplicationConfig extends WebMvcConfigurationSupport { /** * 添加静态资源文件 * @param registry */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/static/img").addResourceLocations("/img/"); registry.addResourceHandler("/static/css").addResourceLocations("/css/"); registry.addResourceHandler("/static/js").addResourceLocations("/js/"); registry.addResourceHandler("/static/swf").addResourceLocations("/swf/"); registry.addResourceHandler("/static/media").addResourceLocations("/media/"); } /** * 添加自定义的Converters和Formatters. */ @Override protected void addFormatters(FormatterRegistry registry) { //添加字符串转换Date的自定义转换器 registry.addConverter(new StringToDateConverter()); } }
WebMvcConfigurationSupport使用方法
最新推荐文章于 2024-06-04 20:24:21 发布