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
" YYYY-MM-DD " ) Private
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
@InitBinder publicvoid initbinder (Webdatabinder binder) { New SimpleDateFormat ("yyyy-mm-dd"); Dateformat.setlenient (false); Binder.registercustomeditor (Date. class New 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:
<%@ 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:
<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
Springmvc of date type conversion problem processing method induction