Data type converter SpringMVC

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
Here Insert Picture Description

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.

  1. First custom type converter classes DateConverter, and added to the Spring IoC container;
    Here Insert Picture Description

  2. Register a custom type converter by the converters properties ConversionServiceFactoryBean

  3. The custom ConversionService registered to Spring MVC in the context of
    step 23 shown below:
    Here Insert Picture Description
    Next, we tested

This is our vo, a package of about

Here Insert Picture Description

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
Here Insert Picture Description

Published 101 original articles · won praise 3 · Views 2234

Guess you like

Origin blog.csdn.net/S_Tian/article/details/104502900