-1:Converter 一个我们一直在使用却不知道的接口
0:Converter接口 接口作用:
当添加了一个实现converter接口的类,并添加到spring容器时。Controller接收的数据会自动转换为想要的类型
1:Converter的完整路径是
org.springframework.core.convert.converter.Converter
2:使用方法
public class ConverTest implements Converter<String, Date>
自定义类实现 Converter接口,String 是原始数据类型,Date是要转换的数据类型。重写convert方法,并加上@Service注解
比如 在重写的方法中 你用 SimpleDateFormat 把格式 2019-01-23 的数据转换date
3:这时前端 传值 2019-01-23 ,就可以直接用date类型接收。
4:springmvc其实已经实现了许多类型转换,比如前端传 1,用 int 就能接收到,其实这就是用了自带的Converter,springmvc同样自带字符串时间转换,默认类型是 2019/01/23 ,还有字符串用“,”分隔,就可以用字符串数组接收。
来源:oschina
链接:https://my.oschina.net/u/3394093/blog/3111914