Sharing of form data processing skills based on custom editor in spring MVC

The object-oriented programming method greatly facilitates the energy spent by programmers in managing data. In the web development process based on spring MVC, the data submitted by the form can be managed by object mapping instead of extracting it from the request one by one. In addition, this function also supports the mapping of basic data types. For example, in, long, float, and so on. In this way, we can get rid of the traditional single string type. However, the application is flexible. Our demand for data is ever-changing. Sometimes we need to handle the data of the form in a compatible way.

For example, date format compatibility:

Yyyy MM DD format is used for date marking in China, and mm / DD / yyyy format is used in Europe and America. Although both formats are date annotation methods, we often have to make cumbersome conversion in order to achieve compatibility.

For example, price compatibility:

The price is nothing more than a string of numbers. We often use the expression of 0.00, and for the price with large amount, we are also used to using the price expression of 0000.00 separated by commas.

In fact, this problem has been considered in spring MVC. In the controller, an editor can be registered when initializing the binding. When the data submitted from the form is mapped to a specific type (or even a specific parameter), it can be converted according to the user-defined method. (except for the data transmitted in binary mode, we generally think that all the parameters transmitted, no matter what they are, are regarded as strings.)

Below I make up a requirement:

I have a form that needs to fill in user name, birthday and points. This represents string type, date type and long type respectively. Here is the form content:

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
分享
二维码
< <上一篇
下一篇>>