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
- @DateTimeFormat (pattern = "Yyyy-mm-dd")
- 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
- @InitBinder
- Public void Initbinder (Webdatabinder binder) {
- SimpleDateFormat DateFormat = new SimpleDateFormat ("Yyyy-mm-dd");
- Dateformat.setlenient (false);
- 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
- <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="FMT"%>
- <fmt:formatdate value="${job.jobtime}" pattern="Yyyy-mm-dd HH:mm:ss"/>
Freemarker Template Engine Method:
[Java]View PlainCopy
- <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