自定義的類型轉換器的實作流程:String-User
1)寫一個類型轉換器。
I、Converter:S,原類型;T代表目标類型
~~~
public class StringToPersonrConverter implements Converter {
// tom-12-100.00
public Person convert(String source) {
// TODO Auto-generated method stub
String array[] = source.split("-");
if (array != null && array.length == 3) {
Person person = new Person();
person.setName(array[0]);
person.setAge(Integer.parseInt(array[1]));
person.setSalary(Double.parseDouble(array[2]));
return person;
}
return null;
}
}
II、ConerterFacotry:
III、GenericConverter:
~~~
2)注冊類型轉換器:
ConversionServiceFactoryBean,spring自動識别IOC容器中conersionService。資料綁定的時候,spring自動的調用對象。
~~~
//在springmvc中配置
~~~
3)conversionService:告知springmvc
~~~
~~~
~~~
Person:
~~~
TOM-12-20.2
Person [name=TOM, age=12, salary=20.2]