1.写一个类实现Converter接口,第一个参数为要转换的类型,第二个参数为转换为什么类型,这里是将String类型转换为Date类型
public class DateConvector implements Converter<String, Date> {
public Date convert(String date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date result = null;
try {
result = sdf.parse(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
}
2.在Spring-mvc.xml文件中将该自定义类型转换器定义在上下文
3.此时如果uri地址为…..?today=2016-09-11这种类型的字符串注入的时候,就会自动转换为Date类型