Data type conversions
1. The custom type converter implements the Converter<s,t> interface and joins it into the SPRINGMVC IOC container,
1 /*2 @Compent added to the IOC container3 Data type converter Myconwerter class inherits converter< primitive type, target type >4 */5 @Component6 Public classMyconverterImplementsConverter<string, student>{7 @Override8 PublicStudent Convert (String source) {9 if(source!=NULL ) {TenString[] split= source.split ("-"); One if(split.length==2) { AString name=split[0]; -Integer Gender=integer.parseint (split[1]); -Student stu=NewStudent (); the stu.setname (name); - Stu.setgender (gender); - returnStu; - } + } - return NULL; + } A }
1 // the Student object class to convert to 2 Public class Student {3 Private String name; 4 Private Integer gender; 5 ....... 6 }
2. Configure the custom converter to the Formattingconversionservicefactorybean factory!
1 <!--the service again as a property of the Annotation-driven exist! -2 <Mvc:annotation-drivenConversion-service= "Service"></Mvc:annotation-driven>3 4 <!--Configure Service -5 <BeanID= "Service"class= "Org.springframework.format.support.FormattingConversionServiceFactoryBean">6 < Propertyname= "Converters">7 <Set>8 <refBean= "Myconverter"/>9 </Set>Ten </ Property> One </Bean>
SPRINGMVC related-type converters