When using SPRINGMVC, you need to convert the date string in the form to the date type of the corresponding JavaBean, and SPRINGMVC does not support the conversion of this format by default, there are two workarounds, as follows:
Method one. Use SPRINGMVC annotations @initbinder
and spring-brought classes in a controller that requires a date conversion WebDateBinder
.
/* The following information is from the network */
The @InitBinder
Annotated controller method allows you to configure WEB data binding directly in your controller class. The @InitBinder
method that marks the initialization WebDataBinder
, which WebDataBinder
is used to populate the commands and form object parameters of the annotated processing method.
?? These initialization binder (Init-binder) methods support @RequestMapping
all parameters supported by the method, processing command/form objects, and related validation result objects. The initialization binder methods must have no return value, so they are usually declared void. Typical parameters include WebDataBinder
and WebRequest
or java.util.Locale
are editors that allow code to register a particular context (context-specific editors).
/* Above information from the network */
Webdatabinder is used to bind the request parameter to the specified property editor. Since the value of the foreground to the controller is of type string, when the data set in the model is a date type, spring will find the corresponding editor for the conversion. Convert the string type to a date type, and then set it in.
To do the type conversion, we need to configure the SPRINGMVC in the SPRINGMVC, which provides many types of converters, which are configured as follows:
<!--Date Conversion - <Beanclass= "Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> < Propertyname= "Messageconverters"> <List>
<!--Configuring a string-type message Converter- <Beanclass= "Org.springframework.http.converter.StringHttpMessageConverter"> < Propertyname= "Supportedmediatypes"> <List> <value>text/html; Charset=utf-8</value> </List> </ Property> </Bean>
<!--Here you can also define other types of message converters-- </List> </ Property> </Bean>
The initialization binder method is then created in a controller that requires a type conversion, and the received string is converted to a date with the corresponding editor, with the following code:
@Controller Public class Myformcontroller { @InitBinder protectedvoid Initbinder (webdatabinder Binder) { new SimpleDateFormat ("Yyyy-mm-dd"); Dateformat.setlenient (false); Binder.registercustomeditor (Date. class New false )); }}
Spring MVC Registers These editors before binding the data, and Spring provides many of the editor's implementation classes, such as Customdateeditor, Custombooleaneditor,customnumbereditor, etc. Use time to invoke Webdatabinder's Registercustomeditor method.
It is important to note that this method of initializing the binding only takes effect within that controller.
In this way, the string date information passed by our foreground can be directly received using date in the background JavaBean.
Method Two: Using the @datetimeformat annotation method, recommended, this method is simple, and the code quantity is small
First, @DateTimeFormat annotations are used in the Joda package, we need to introduce Joda related jar packages. If you do not introduce an exception
<Dependency> <groupId>Joda-time</groupId> <Artifactid>Joda-time</Artifactid> <version>2.9.7</version></Dependency>
We then add annotations to the date type data to be received in JavaBean:
Public class person{ private String name; Private Integer age; Private Integer sex; // add annotations to the date type and specify the received format style @DateTimeFormat (pattern= "Yyyy-mm-dd") private Date birthday;}
Finally, a note-driven configuration is made in the SPRINGMVC configuration XML file:
<!--Date Conversion - <Mvc:annotation-drivenConversion-service= "Conversionservice" /> <BeanID= "Conversionservice"class= "Org.springframework.format.support.FormattingConversionServiceFactoryBean"></Bean> <Beanclass= "Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> < Propertyname= "Messageconverters"> <List> <Beanclass= "Org.springframework.http.converter.StringHttpMessageConverter"> < Propertyname= "Supportedmediatypes"> <List> <value>text/html; Charset=utf-8</value> </List> </ Property> </Bean> </List> </ Property> </Bean>
This completes, subsequent as long as the new JavaBean in the date type plus @datetimeformat annotations, any contorller in the use of JavaBean to accept data, the passed string type data is automatically converted to the corresponding date type
It is important to note that the date format that receives the string needs to be consistent with the format defined after the @datetimeformat annotation.
SPRINGMVC handling the use of date type data @initbinder @DateTimeFormat annotations