PDA

View Full Version : CustomCollectionEditor and date formatting


kanonmicke
Jun 15th, 2007, 09:07 AM
Hi.

I have a List witch holds Car classes with two Date variables
class Car{
Date dateOne;
Date dateTwo;
...
}

Formatting dates in the gui works fine with the customDateEditor in my controllerclass:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));

However...
Upon submit the date transform to 'long' date and is unformatted.
The unformatted date shows up in the gui, my successView is the same as my formView...

I'm using spring el language and do the listing of the carList like this:
<c:forEach items="${command.carList}" varStatus="status">
<form:input path="carList[${status.index}].dateOne" />
</c:forEach

Can anyone give me a pointer on how to edit so the date transform into my preferable format?

Thanks!

pmularien
Jun 15th, 2007, 10:52 AM
Please post the return statement from your onSubmit method. The common cause of this is that you are not using errors.getModel() as the base for the model that you return from onSubmit.

sami25
Jun 15th, 2007, 10:56 AM
Hi,

use the following.


<fmt:parseDate value="${Date}" pattern="yyyyMMddHHmmss" var="parsedModificationDate"/>
<fmt:formatDate value="${parsedModificationDate}" pattern="dd-MM-yyyy" />

Jörg Heinicke
Jun 16th, 2007, 08:19 PM
A short search on the forum with "custom editor date" reveals the following two threads where I have guided others through this issue:
http://forum.springframework.org/showthread.php?t=38541
http://forum.springframework.org/showthread.php?t=38585
So Peter is correct. Recommending to use fmt:formatDate is really bad since you don't get the date back into your form bean as the string can't be parsed.

Jörg

kanonmicke
Jun 18th, 2007, 03:18 AM
Thanks Peter, it did the trick,

When I changed my return statement from my onSubmit method in the controller class it all works fine.
Cannot use the fmt in the jsp because then it will not bind to the model again.

Before: return new ModelAndView(getSuccessView()).addObject("command",aCommand);

Now: return new ModelAndView(getSuccessView(),errors.getMap());

So, in some strange way the binder method transform the errors object but not the command object variabels after exiting from the onSubmit method...

Jörg Heinicke
Jun 18th, 2007, 07:26 PM
Now: return new ModelAndView(getSuccessView(),errors.getMap());

So, in some strange way the binder method transform the errors object but not the command object variabels after exiting from the onSubmit method...

The logic is in errors.getModel() or more exact AbstractBindingResult.getErrors(). If you have a look into the resulting map you will see it has the command under its name and an additional entry in it: org.springframework.validation.BindingResult.comma ndName with the BindingResult instance as value. This one is later on retrieved when doing the actual binding during rendering the page. Without that BindingResult instance there are no custom editors available.

Jörg

macar
Nov 14th, 2007, 05:13 PM
Thanks Peter, it did the trick,

When I changed my return statement from my onSubmit method in the controller class it all works fine.
Cannot use the fmt in the jsp because then it will not bind to the model again.

Before: return new ModelAndView(getSuccessView()).addObject("command",aCommand);

Now: return new ModelAndView(getSuccessView(),errors.getMap());

So, in some strange way the binder method transform the errors object but not the command object variabels after exiting from the onSubmit method...

Brilliant summary - thanks! Such helpful posts should be made sticky :D