Why use the data type converter
Spring MVC default data type converter:
These types of converters can perform most Java type of conversion work, you can also customize the type converter to achieve special needs, such as the date string into a Date type.
The following red annotation is our custom type converter
How to define a custom type converter
First, we define what we need: date string into a Date type.
ConversionService Spring is the core of the interface type conversion system. Spring can be defined in the IOC ConversionService a container using ConversionServiceFactoryBean. Spring will automatically recognize the IOC container ConversionService, and use it for data conversion and the Bean properties configuration parameters into Spring MVC bind processing method and so on.
-
First custom type converter classes DateConverter, and added to the Spring IoC container;
-
Register a custom type converter by the converters properties ConversionServiceFactoryBean
-
The custom ConversionService registered to Spring MVC in the context of
step 23 shown below:
Next, we tested
This is our vo, a package of about
When the form is submitted .do transmission request, a transfer layer UserInfo object to the Controller of birth entered here is of type String (Yao An input format of yyyy-MM-dd)
With the above operation
Our output of the Controller layer type Date Time