In depth understanding of spring MVC data transformation

This article mainly introduces the relevant contents of spring MVC data conversion for your reference and learning. Let's take a look at the detailed introduction.

Data binding

Spring MVC is responsible for converting the information in the request in a certain way and binding it to the parameters of the processing method. The processing core of the whole process is completed by databinder. The conversion process is as follows:

1. Databinder obtains parameter information from ServletRequest;

2. Databinder gets the parameters of the processing method;

3. Databinder calls the conversionservice component for data type conversion and data formatting, and fills the conversion results into the parameter object;

4. Databinder calls the validator component to verify the data;

5. After going through the above steps, databinder will generate binderresult object, which contains converted information and verified error information.

data conversion

In the Java language, in Java The beans package provides a propertyeditor interface for data conversion. The core function of propertyeditor is to convert a string into a Java object. Spring has added a common type conversion module, org. 0, since 3.0 springframework. In the convert package, conversionservice is org springframework. As the core component of the convert package, you can customize a conversionservice in the context of spring by using the conversionservicefactorybean. Spring will automatically recognize the conversionservice and use it during parameter conversion in spring MVC. The configuration example is as follows:

Spring MVC supports not only the new converter framework, but also the property editor of JavaBeans. You can use @ initbinder to add a custom editor in the controller.

Examples are as follows:

Priority of various converters:

1. Query the editor defined by @ initbinder;

2. Query the custom converter assembled through conversionservice;

3. Query the global custom editor assembled through the webbindinginitializer interface.

Formater

Except org springframework. core. convert. There are three types of converter interfaces defined in the converter interface. Spring MVC is available at org springframework. Some format conversion interfaces are also provided in the format package. The biggest difference between format and converter is that converter implements the conversion from object to object, while format implements the conversion from string to object. The most important interface in the format package is formater. An example of using formater is as follows:

Finally, the dateformatter is injected into the conversionservice. The injection method is the same as that of the converter. It can also be found that conversionservice is the core of data conversion.

Annotation for format

At org springframework. format. Two annotations are defined in the annotation package, @ datetimeformat and @ numberformat, which can be used on attributes in the domain. When spring MVC processes parameter binding data and model data output, it will automatically apply formatting through annotations.

summary

The above is the whole content of this article. I hope the content of this article has a certain reference value for your study or work. If you have any questions, you can leave a message. Thank you for your support for programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>