Reputation: 9765
Below is the code for datepicker on an input
element with id as endDate
$('#endDate').datepicker({
changeMonth: true,
changeYear: true,
dateFormat: 'mm/yy',
});
It removes the date part and only month and year is shown in the format of 'MM/YYYY'.
I have binded this element to a beans property as shown below.
<form:input id="endDate" onkeydown="return false;" readonly="readonly" path = "endDate" />
In the beans property i have used org.springframework.format.annotation.DateTimeFormat
as shown below
@DateTimeFormat(pattern = "MM/yyyy")
private Date endDate;
The datepicker works fine as expected, however i get the following exception when form is submitted to controller.
org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 2 errors
Field error in object 'SpringWeb' on field 'endDate': rejected value [12/2014]; codes [typeMismatch.SpringWeb.endDate,typeMismatch.endDate,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [SpringWeb.endDate,endDate]; arguments []; default message [endDate]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'endDate'; nested exception is org.springframework.core.convert.ConversionFailedException: Unable to convert value "12/2014" from type 'java.lang.String' to type 'java.util.Date'; nested exception is java.lang.IllegalArgumentException: Invalid format: "12/2014" is malformed at "14"]
Field error in object 'SpringWeb' on field 'startDate': rejected value [12/2014]; codes [typeMismatch.SpringWeb.startDate,typeMismatch.startDate,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [SpringWeb.startDate,startDate]; arguments []; default message [startDate]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'startDate'; nested exception is org.springframework.core.convert.ConversionFailedException: Unable to convert value "12/2014" from type 'java.lang.String' to type 'java.util.Date'; nested exception is java.lang.IllegalArgumentException: Invalid format: "12/2014" is malformed at "14"]
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.doBind(HandlerMethodInvoker.java:810)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:359)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:171)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:436)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
at java.lang.Thread.run(Unknown Source)
What is the reason ? What can be solution ?
Upvotes: 0
Views: 1003
Reputation: 144
To solve this problem I implemented a InitBinder method in the controller, registering a custom editor for the specific field:
@InitBinder
public void binder(WebDataBinder binder) {
DateFormat dateOnlyFormat = new SimpleDateFormat("dd/MM/yyyy");
dateOnlyFormat.setLenient(true);
binder.registerCustomEditor(Date.class, "dataInicio", new CustomDateEditor(dateOnlyFormat, true));
}
Upvotes: 0
Reputation: 15698
From your stacktrace you can see message Invalid format: "12/2014" is malformed at "14"
You need to change your pattern for private Date endDate
from "MM/yyyy"
to "MM/yy"
because yyyy
means year expressed in 4 digits like 2014
however you are passing year in 2 digits 14
Upvotes: 1