For the MVC framework, parameter binding has always been a magical and convenient thing, using the property editor, type converter in the process of parameter binding
Parameter binding process
Parameter binding: Converts the data in the request into an object of the specified type, to the method of processing the request
- Request access to Disptacherservlet, remove data from request
- Disptacherservlet sending the data in the request to the controller
- Gets the type of parameter that the controller needs to receive, sends the parameter type and the request data to the DataBinder
- DataBinder sends the parameter type and request data to TypeConverter, which is assembled by TypeConverter into a bean
- TypeConverter finds registered PropertyEditor in propertyeditorregistry based on the type of member in the Bean
- PropertyEditor data setter into members of the bean
- TypeConverter the assembled beans back to the DataBinder
- DataBinder the assembly bean to the method of processing the request
In the process of parameter binding TypeConverter and PropertyEditor are the most core data into objects (non-serialized) process TypeConverter is responsible for transforming the data into a beanpropertyeditor responsible for transforming the data into a member field
Property Editor
Propertieseditor is responsible for converting simple objects, because HTTP requests are in the form of strings, so it is generally a string to convert SPRINGMVC to provide many default property editors. In the Org.springframework.beans.propertyeditors package, for example
- Custombooleaneditor.class,string Convert Boolean
- customcollectioneditor.class,string Conversion Collection
- Customdateeditor.class,string Convert Date
- custommapeditor.class,string Conversion Map
- customnumbereditor.class,string convert int, floot, double.
All of the property editors are inherited Propertieseditorsupport, the default property editor, and spring will load automatically when it is started. In addition, if the properties you want to assemble do not have an appropriate editor, you can also customize the property editor after registering the custom attribute editor. This property editor can be used globally in Customeditorconfigurer because the factory of the property editor is the global scope
Propertieseditor Source Code Analysis
Propertieseditor.java
public class Propertieseditor extends PropertyEditorSupport {// Turns a String into an object of the specified type @Override public void Setastext (string text) throws IllegalArgumentException {Properties props = new Pr Operties ();//properties with key-value stored value if (text! = null) {try {//Key=value or key:value information represented in string into properties//k EY represents the name of the field in the bean//if it is to be converted to date, value is the form of "date=2012-12-12" (date is a field name) Props.load (new date,string) Bytearrayinputstream (Text.getbytes ("iso-8859-1")); } catch (IOException ex) {throw new IllegalArgumentException ("Failed to parse [" + text + "] into Properties", ex); }} setValue (props); }//Converts an old object into a new object @Override public void SetValue (object value) {if (!) ( Value instanceof Properties) && value instanceof Map) {Properties props = new Properties (); Props.putall ((map<?,? >) value); Super.setvalue (props); The else {//parent class PropertyEditorSupport holds the value object, which is the object to be converted Super.setvalue (value); } }}
It is important to note that Setastext uses a certain format string to achieve the effect of attribute editing,"member name =value", or "member Name: Value", so that the value Set to the specified member of the bean the two most important methods in the editor are Setastest (String) and SetValue (value), which are done in both methods from String--object,object--object
Customdateeditor Source Code Analysis
Customdateeditor is a default property editor for spring, which is responsible for converting a string to a Date object in the specified format as well as he inherited the Propertieseditorsupport, overriding the Setastest method
public class Customdateeditor extends PropertyEditorSupport {//specified date format, such as "YYYY-MM-DD" private final DateFormat DateFormat; Whether to allow the string to be empty private final Boolean allowempty; Strict date length private final int exactdatelength; Public Customdateeditor (DateFormat DateFormat, Boolean allowempty) {//constructor method} public Customdateeditor (DateFormat DateFormat, Boolean allowempty, int exactdatelength) {//constructor method}//string converted to dtae @Override public void Setastext (String te XT) throws IllegalArgumentException {//Determine if the string is empty if (This.allowempty &&!) Stringutils.hastext (text)) {setValue (null); }//Determine if the string length equals exactdatelength else if (text! = null && this.exactdatelength >= 0 && text.length () ! = this.exactdatelength) {throw new IllegalArgumentException ("Could not parse date:it are not exactly" + This.ex Actdatelength + "characters long"); } else {try {//text is formatted as a Date object SetValue (This.dateFormat.parse (text)); } catch (ParseException ex) {throw nEW illegalargumentexception ("Could Not parse Date:" + ex.getmessage (), ex); }}}//output string from date @Override public string Getastext () {Date value = (date) getValue (); Returns the formatted string return (value! = null? This.dateFormat.format (value): "");}}
From the source of the customdateeditor can be seen, the most important thing is to rewrite the Setastext method, first check the string format character does not meet the requirements, do not meet the requirements of the exception, and then the string to the specified DateFormat date object
Type converters
The new property editor is used to populate the Bean's properties, and the type converter is responsible for converting from the data to a bean so in the process of conversion, the property editor is required to fill in the properties, so you should hold a bunch of property editors (beans have a variety of properties), Then hold a propertyeditorregistry (a property editor factory) can be the implementation of the type converter is not as many as the property editor, mainly three
- TypeConverter, type-converted Interface
- Typeconvertersupport, the implementation of type conversion, holds a typeconverterdelegate, the specific conversion work is given to typeconverterdelegate complete
- Typeconverterdelegate, type conversion of the delegate class, all types of conversion work is done by him
The method to be implemented is only convertifnecessary, which is converted from the source object to the target object
Typeconverterdelegate Source Code Analysis
Because the conversion work is responsible for the typeconverterdelegate, the source code is too long, just look at the conversion of that part
/* @Param The name of the PropertyName Bean @Param requiredtype required type @Param TypeDescriptor type descriptor */public <T> T Convertifnecessary (String PropertyName, Object OldValue, Object newvalue,class<t> requiredtype, TypeDescriptor TypeDescriptor) throws IllegalArgumentException {//Obtain a property editor that can edit requiredtype from the Registered property Editor PropertyEditor editor = This.propertyEditorRegistry.findCustomEditor (Requiredtype, PropertyName); ...//Use the attribute editor to convert OldValue to requiredtype convertedvalue = Doconvertvalue (OldValue, Convertedvalue, Requiredtype, Editor); ... return convertedvalue; }/* @Param The name of the PropertyName Bean @Param requiredtype Required Type @Param Editor Property editor */Private object Doconvertvalue (object OldValue, Object newvalue, Class<?> requiredtype, PropertyEditor editor) {Object convertedvalue = newvalue; If editor! = NULL &&! ( Convertedvalue instanceof String) {try {//Convert data Editor.setvalue (convertedvalue); Get the converted data Object Newconvertedvalue = Editor.getvalue (); if (newconvertedvalUE = Convertedvalue) {convertedvalue = Newconvertedvalue; editor = null; }} catch (Exception ex) {if (logger.isdebugenabled ()) {Logger.debug ("propertyeditor [" + Editor.getclass (). GE Tname () + "] does not support SetValue call", ex); }}} Object returnvalue = Convertedvalue; ... return returnvalue; }}
View Original: http://zswlib.com/2016/07/16/springmvc%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2%E5%99%A8%E3%80%81%E5%B1%9E%E6% 80%a7%e7%bc%96%e8%be%91%e5%99%a8/
SPRINGMVC type converter, property editor