We have a web application, which has among other things a date date picker bind to fields "fromDate" and "toDate". The date picker displays the date format according the selected language (also in front end) (For example as "dd.MM.yyyy" ). After the date interval was selected, some controller must be called to handle it.
The controller example:
@RequestMapping(value = "/performSomeOperation", produces = "application/vnd.ms-excel")
@ResponseBody
public FileSystemResource getData(@Valid DateParameter interval, BindingResult result, HttpServletRequest request, HttpServletResponse response) {....
}
I use currently following annotation public class DateParameter {
@DateTimeFormat(pattern="dd.MM.yyyy")
private Date fromDate;
@DateTimeFormat(pattern="dd.MM.yyyy")
private Date toDate;
}
If i define the interval NOT in "dd.MM.yyyy" format and try perform the action, the 400 BAD REQUEST will be returned After debugging of spring servlet org.springframework.web.servlet.DispatcherServlet.doService(HttpServletRequest, HttpServletResponse)
i found the following exception as return value
[XrefExportParameter(fromDate=null, toDate=null), org.springframework.validation.BeanPropertyBindingResult: 2 errors Field error in object 'dateParameter' on field 'fromDate': rejected value [25.02.2015]; codes [typeMismatch.dateParameter.fromDate,typeMismatch.fromDate,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [dateParameter.fromDate,fromDate]; arguments []; default message [fromDate]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'fromDate'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type java.util.Date for value '25.02.2015'; nested exception is java.lang.IllegalArgumentException]
i suppose, that statical definition of date format is a root of current problem.
I seek for "smart" solutions;-) og current problem.
We have in system several languages/countries with different date formats.
So the solutions a-la register custom date editors via web binder according to language/country would be very expensive.
Aucun commentaire:
Enregistrer un commentaire