The data binding process is as follows:
The 1.SPRINGMVC main frame passes an instance of the ServletRequest object and the target method to the Webdatabinderfactory instance to create the DataBinder instance object
2.DataBinder calls the Conversionservice component assembled in the spring MVC context for data type conversion, formatting, and populating the request information in the servlet into the Parameter object
3. Call the validator component to verify the data legitimacy of the incoming parameter object that already has the request message bound, and eventually generate the data-bound result BindingData Object
4.spring MVC extracts the Bindingresult and checksum error objects from the, and assigns them to the response of the processing method in the parameter
I. Data conversion
1.SPRINGMVC is automatically matched by the property name of the request parameter and the name of the Pojo property. Cascading properties are supported.
2. Custom Converters
2.1 Custom Converter Implementation converter<s,t>
@Component Public classEmployeeconverterImplementsConverter<string, employee>{@Override PublicEmployee Convert (String source) {System.out.println ("Employeeconverter."); if(Source! =NULL) {string[] args= Source.split (","); Integer ID= Integer.parseint (args[0]); String username= Args[1]; String Email= Args[2]; Employee Employee=NewEmployee (ID, username, email); System.out.println (Source+ "----convert----" +employee); returnemployee; } return NULL; } }
2.2 In the SPRINGMVC configuration file, configure the following
<Mvc:annotation-drivenConversion-service= "Conversionservice"></Mvc:annotation-driven>
<BeanID= "Conversionservice"class= "Org.springframework.context.support.ConversionServiceFactoryBean"> < Propertyname= "Converters"ref= "Employeeconverter"></ Property> </Bean>
Two. Data formatting
The input data of the foreground is passed through the string type to the background, if the attribute of the entity is the basic type, then it can be converted normally,
However, if some of the properties are java.util.Date or float, you do not know what format to convert, this is the following annotation tag to tell the server what format to convert
@DateTimeFormat (pattern= "Yyyy-mm-dd")private Date birth;
@NumberFormat (Pattern= "#,###,###.#")privatefloat salary;
The current input data does not meet the above format, the conversion results can be in the method parameters of the target method, injected Bindingresult result to obtain, such as the following
@RequestMapping (value= "Testconverter", Method={requestmethod.post}) Span style= "color: #0000ff;" >public String Testconverter (employee employee, bindingresult result) {System. Out.println ( "Springmvctest.testconverter" ); if (Result.geterrorcount () > 0) {Li St <FieldError> fielderrors = Result.getfielderrors (); for (Fielderror error:fielderrors) { System.out.println (Error.getfield () + ":" + Error.getdefaultmessage ()); }} System.out.println (employee); return "Success" ; }
Three. Data verification
JSR303 is a specification for Java Bean validation, but SPRINGMVC does not implement it, and Hibernate validator alignment is implemented.
The specific process is as follows:
1. Add a jar package to hibernate validator
2. Annotations on the bean corresponding to the required validation
3. Background to error message, Bindingresult
4. The foreground gets the error message, <form:errors path= "property name" ></form:errors>
5. How to customize the message,
Configuring in the SPRINGMVC configuration file
<class= "Org.springframework.context.support.ResourceBundleMessageSource" > < name= "basename" value= "i18n"></Property ></beans>
SRC Add the I18n.properties property file, the key value pair how to write (validation with the annotation. Entity name. Attribute NAME = message)
Precautions,
In the SPRINGMVC configuration file, you must have already configured the
<></mvc:annotation-driven>
There is also the target method of the validation object is preceded by @valid, will verify
springmvc--Data Conversion & Data formatting & data validation