Spring has two types of automatic type converters, one is converter and the other is propertyeditor.
The difference between the two: converter is a type converted to a type, Editor: Converting from String type to other type.
In a way, converter contains editor. If there is a need to convert from a string to another type. Preferred editor.
Converter Code Show:
Implements a string type conversion of date.
Myconverter class
Public classMyconverterImplementsConverter<string, date> { PublicDate Convert (String source) {System.out.println ("Into the converter."); //Creating a type converter@SuppressWarnings ("Unused") SimpleDateFormat SimpleDateFormat=Getsimpledateformat (source); Date Date=NULL; Try{Date=simpledateformat.parse (source); } Catch(ParseException e) {e.printstacktrace (); } returndate; } PrivateSimpleDateFormat Getsimpledateformat (String source) {SimpleDateFormat SimpleDateFormat; if(Pattern.matches ("^\\d{4}-\\d{2}-\\d{2}$", source)) {SimpleDateFormat=NewSimpleDateFormat ("Yyyy-mm-dd"); } Else if(Pattern.matches ("^\\d{4}/\\d{2}/\\d{2}$", source)) {SimpleDateFormat=NewSimpleDateFormat ("Yyyy/mm/dd"); } Else if(Pattern.matches ("^\\d{4}\\d{2}\\d{2}$", source)) {SimpleDateFormat=NewSimpleDateFormat ("YyyyMMdd"); } Else { Throw NewTypemismatchexception ("", Date.class); } returnSimpleDateFormat; }}
Controller class
@Controller @requestmapping ("/student") Public classStudentcontroller {@RequestMapping ("/add") Publicmodelandview Add (Student Student) {System.out.println (Student.getname ()); System.out.println (Student.getbirthday ()); return NewModelandview ("Success"); } @ExceptionHandler PublicModelandview Exceptionmethod (Exception ex, Modelandview MV, httpservletrequest request) {System.out.println ( /c7>"Enter new Interface"); //get information entered by the foregroundString name = Request.getparameter ("name"); String Birthday= Request.getparameter ("Birthday"); String message=Ex.getmessage (); if(Message.contains (name)) {Mv.addobject ("Nameerro", "User name entered incorrectly"); } if(Message.contains (birthday)) {Mv.addobject ("Birthdayerro", "date entered incorrectly"); } mv.addobject ("Name", name). AddObject ("Birthday", birthday). Setviewname ("index"); returnMV; }}
Tangled up, or decided to write the editor code, and then play a game of bucket Luo, I hope many years later, I can still like this games.
Embarrassed, the original can also add line number ...
1 @Controller2@RequestMapping ("/student")3 Public classStudentcontroller {4@RequestMapping ("/add")5 Publicmodelandview Add (Student Student) {6 System.out.println (Student.getname ());7 System.out.println (Student.getbirthday ());8 return NewModelandview ("Success");9 Ten } One A /** - * Binder.registercustomeditor initialization parameter binding newcustomdateeditor: Create Type editor True - * Allow date format to be empty the * - */ - @InitBinder - Public voidInitbinder (Webdatabinder binder) { +Binder.registercustomeditor (Date.class,Newmyeditor ()); - } +}
Public classMyeditorextendsPropertieseditor {@Override Public voidSetastext (String Source)throwsillegalargumentexception {simpledateformat sdf=getDate (source); Date Parse=NULL; //Type Conversions Try{Parse=sdf.parse (source); SetValue (parse); } Catch(ParseException e) {e.printstacktrace (); } } /** * @paramSource * String that is passed to the date format **/ PrivateSimpleDateFormat getDate (String source) {SimpleDateFormat SDF=NewSimpleDateFormat (); //Judging if(Pattern.matches ("^\\d{4}-\\d{2}-\\d{2}$", source)) {SDF=NewSimpleDateFormat ("Yyyy-mm-dd"); } Else if(Pattern.matches ("^\\d{4}/\\d{2}/\\d{2}$", source)) {SDF=NewSimpleDateFormat ("Yyyy/mm/dd"); } Else if(Pattern.matches ("^\\d{4}\\d{2}\\d{2}$", source)) {SDF=NewSimpleDateFormat ("YyyyMMdd"); } Else { /*** None Match let it throw typemismatchexception exception public * typemismatchexception (Object value, class<? > Requiredtype) {* Vallue value can correspond to Requiredtype type there will be no exception we'll have to write one that can't be converted.*/ Throw NewTypemismatchexception ("", Date.class); } returnSDF; }}
Self-feeling: Editor code less than controller, but still can't remember ...
I went to the bucket, hahaha ...
Take a closer look, correct, not the controller code is much, set the echo on the page, so the code is much more. But still not quite understand, return, date Date=null,date parse=null. After the afternoon discussion, write the feeling.
Spring automatic type conversion ========converter and PropertyEditor