SPRINGMVC and MyBatis Integration date format conversion

Source: Internet
Author: User

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.

    • Dateconverter.java
 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.