SPRINGMVC three treatment methods for date type conversion problem

Source: Internet
Author: User
Tags dateformat

Objective

We in the SPRINGMVC development, may encounter more problems is the foreground and background entity class date conversion processing problem, said the problem is not big, but many people often encounter this problem in development, sometimes very headache, there is time problem is not obvious, and then gradually tracking the problem, It is found that the date type conversion failed "mapping" is not caused by the date attribute of the corresponding persistent class, and I have also written a blog post: "Bad Request" appears in Springmvc Error (using @responsebody to handle the JSON data passed from Ajax to the Bean) is a workaround. Interested in the code farmers can take a look, summed up the common cause of SPRINGMVC "three bad Request" error seven reasons, including the foreground string date type and background Java date type does not match.

This evening, I deliberately took the time to summarize the three ways to solve the problem, to share with you to help more people like me. My humble, if the process has explained the unclear or wrong place, but also hope to seriously read you, leave your valuable comments or suggestions, so you, me, and everyone faster and better common progress!

All right, let's cut to the chase!

Method One: Add date formatting annotations in entity classes [Java]View PlainCopy
    1. @DateTimeFormat (pattern = "Yyyy-mm-dd")
    2. Private Date Receiveapptime;

As above, on the corresponding attributes, plus the specified date format of the annotations, I personally tested, easy to solve the problem!

Method Two: Add a data binding code to the Controller action [Java]View PlainCopy
    1. @InitBinder
    2. Public void Initbinder (Webdatabinder binder) {
    3. SimpleDateFormat DateFormat = new SimpleDateFormat ("Yyyy-mm-dd");
    4. Dateformat.setlenient (false);
    5. Binder.registercustomeditor (Date.    Class, new Customdateeditor (DateFormat, true)); //true: Allow null value to be entered, false: cannot be null

Method Three: Implement a global date type converter and configure it

This method is more complicated, please check my blog: SPRINGMVC Configuring the global date Converter to handle date conversion exceptions

Additional method Four: Fit the page to convert the date type to a string and jsp,freemark the page

JSP Template Engine Method:

[Java]View PlainCopy
    1. <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="FMT"%>
    2. <fmt:formatdate value="${job.jobtime}" pattern="Yyyy-mm-dd HH:mm:ss"/>

Freemarker Template Engine Method:

[Java]View PlainCopy
    1. <input id="receiveapptime" name="Receiveapptime" type="text" value="${(bean.receiveapptime?) String (' Yyyy-mm-dd '))!} " />

OK, finished, hope the above method can help you are reading!

Reprint please indicate-java my life (Chen Leixing) Source: http://blog.csdn.net/chenleixing/article/details/44708533

Finally, seriously read the netizens, the great gods, if there is a feeling I this program ape has a place to say wrong or wrong or you have a good proposal or suggestions or ideas, but also look You kindness alms n seconds to leave your valuable text (message), so that you, I, and the vast number of programs apes grow and Progress faster ....

SPRINGMVC three treatment methods for date type conversion problem

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.