In the previous blog "Springmvc and MyBatis Integration (a)--query staff list" left the date format conversion problem, in this document resolution process.
for a Pojo object in a controller parameter, a custom parameter binding is required if there is a date type in the attribute.
The request date data string is passed to a date type, and the type of the date to be converted is consistent with the types of date attributes in the Pojo.
In the example program in the previous article, the person class attribute is as follows:
And the test results are as follows:
So the custom parameter binding turns the date string into the Java.util.Date type. A custom parameter binding component needs to be injected into the processor adapter.
Add Part One: Custom Date type bindings.
PackageConverter;Importjava.text.ParseException;ImportJava.text.SimpleDateFormat;Importjava.util.Date;ImportOrg.springframework.core.convert.converter.Converter; Public classDateConverterImplementsConverter<string, date>{ PublicDate Convert (String source) {//implement convert date string to date type (format is YYYY-MM-DD HH:mm:ss)SimpleDateFormat SimpleDateFormat=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); Try { //turn directly back returnsimpledateformat.parse (source); } Catch(ParseException e) {e.printstacktrace (); } //If parameter binding fails, returns null return NULL; }}
Modify Part Two: How to configure
Add the following code to the Spring-mvc.xml:
<conversion-service= "Conversionservice"></MVC: Annotation-driven>
<!--Custom parameter Bindings - <BeanID= "Conversionservice"class= "Org.springframework.format.support.FormattingConversionServiceFactoryBean"> <!--Converters - < Propertyname= "Converters"> <List> <!--Date Type Conversions - <Beanclass= "converter." DateConverter " /> </List> </ Property> </Bean>
Modify Part Three: allperson.jsp
Change <td>${list.in_time}</td> to: <TD><value= "${list.in_time}" pattern = "Yyyy-mm-dd HH:mm:ss"/><td>
The results are as follows:
modified successfully!
SPRINGMVC and MyBatis Integration date format conversion