Custom type converters (example is the date type of the conversion)
1. New type conversion class Dateconvert.java and inherit Java.beans.PropertyEditorSupport
2. Rewrite the Setastext (String text) method
Public classDateconvertextendsPropertyEditorSupport {@Override Public voidSetastext (String text)throwsillegalargumentexception {simpledateformat sdf=NewSimpleDateFormat ("Yyyy-mm-dd"); Try{Date Date=sdf.parse (text); This. SetValue (date); } Catch(ParseException e) {e.printstacktrace (); } }}
3. Configure the type converter in the configuration file
<Beanname= "Customeditor"class= "Org.springframework.beans.factory.config.CustomEditorConfigurer"> < Propertyname= "Customeditors" > <Map> <entryKey= "Java.util.Date"value= "Com.tidus.spring.util.DateConvert"/> </Map> </ Property> </Bean>
4, then you can inject the date type into the model
<Beanname= "Studentservice"class= "Com.tidus.spring.service.StudentService"> < Propertyname= "DAO"ref= "Studentdao" /> < Propertyname= "Studentlist"> <List> <Beanname= "S1"class= "Com.tidus.spring.model.Student" > < Propertyname= "Name"value= "Zhang San" /> < Propertyname= "id"value= "0" /> < Propertyname= "Age"value= " the" /> < Propertyname= "Birthday"value= "1987-05-08" /> </Bean> <Beanname= "S2"class= "Com.tidus.spring.model.Student" > < Propertyname= "Name"value= "Zhang 4" /> < Propertyname= "id"value= "1" /> < Propertyname= "Age"value= "All" /> < Propertyname= "Birthday"value= "1985-03-08" /> </Bean> <Beanname= "S3"class= "Com.tidus.spring.model.Student" > < Propertyname= "Name"value= "Zhang 5" /> < Propertyname= "id"value= "2" /> < Propertyname= "Age"value= "+" /> < Propertyname= "Birthday"value= "1987-06-08" /> </Bean> </List> </ Property> </Bean>
Spring configuration file