View Full Version : Fromatting Date output to forms (binders?)
tnine
Jun 17th, 2007, 11:34 PM
Hi all,
I'm able to successfully change my date format input to MM/dd/yyyy using a CustomDateEditor when the form is posted. However, when the form is displayed I'm getting the default output of the toString method on a java.util.Date object in the format MM-dd-yyyy hh:mm:ss. I'm not sure how to use my binder to also change the output in my form. I'm using the spring "form:input" tag. Is there a way to bind this property editor to my jsp form tags?
cwilkes
Jun 18th, 2007, 12:26 AM
I would look into using JSTL's fmt:formatDate tag.
Jörg Heinicke
Jun 18th, 2007, 07:20 PM
You can search the forum for "custom editor date". Most often the reason is the "wrong" model:
http://forum.springframework.org/showthread.php?t=38541
http://forum.springframework.org/showthread.php?t=38585
http://forum.springframework.org/showthread.php?t=40147
Jörg
tnine
Jun 19th, 2007, 11:35 PM
Hi Jorg. Thanks for the reply. I've looked at the other posts, and its not entirely clear to me how changing my returned ModelAndView results in a different binding of property editors. Here is my basic code for binding.
protected static final CustomDateEditor DATE_EDITOR = new CustomDateEditor(
new SimpleDateFormat("MM/dd/yyyy"), false);
..class definition
/*
* (non-Javadoc)
*
* @see org.springframework.web.servlet.mvc.multiaction.Mu ltiActionController#initBinder(javax.servlet.http. HttpServletRequest,
* org.springframework.web.bind.ServletRequestDataBin der)
*/
@Override
protected void initBinder(HttpServletRequest request,
ServletRequestDataBinder binder) throws Exception {
binder.registerCustomEditor(Date.class, DATE_EDITOR);
super.initBinder(request, binder);
}
Here is my ModelAndView constructor, and the beans I give it. What am I doing wrong when I create my ModelAndView, and can you explain how changing my return changes the binding and property editors used by the form taglib?
/**
* This methods loads data from the id and
*
* @param request
* @param response
* @param vc
* @return
*/
public ModelAndView edit(HttpServletRequest request,
HttpServletResponse response) {
if (request.getParameter(DELETE) != null) {
return delete(request, response);
}
CompanyRevision revision = this.companyManager.getLastRevision(Long
.valueOf(getId(request)));
// TODO didn't find one, return an error
if (revision == null) {
throw new RuntimeException("Whoops, I couldn't find that company");
}
return createEditView("edit", revision, request);
}
/**
*
* @param view
* @param revision
* @param request
* @return
*/
public ModelAndView createEditView(String view, CompanyRevision revision,
HttpServletRequest request) {
List<CompanyStatusType> statuses = this.companyManager
.getCompanyStatusTypes(getCurrentArthurUser(reques t)
.getVentureCapital().getId());
HashMap<String, Object> options = new HashMap<String, Object>();
options.put("companyRevision", revision);
options.put("companyStatusList", statuses);
return new ModelAndView(view, options);
}
Jörg Heinicke
Jun 20th, 2007, 01:59 PM
I've looked at the other posts, and its not entirely clear to me how changing my returned ModelAndView results in a different binding of property editors.
Well, what can I tell you more than that it does? I wrote about the details in the last mentioned thread (http://forum.springframework.org/showthread.php?t=40147#6).
Here is my ModelAndView constructor, and the beans I give it. What am I doing wrong when I create my ModelAndView, and can you explain how changing my return changes the binding and property editors used by the form taglib?
Instead of a new Map you need to pass the one retrieved via errors.getModel() to the ModelAndView constructor or at least one that contains all entries of it. In your case options.putAll(errors.getModel()) should work. Or the other way around:
Map model = errors.getModel();
model.putAll(options);
return new ModelAndView(view, model);
Jörg
tnine
Jun 20th, 2007, 03:23 PM
OK, that makes a bit more sense. However I'm extending a MultiActionController. When I try to use the following method signature, spring blows up. BindException extends exception, how can I get to the errors object to get my model map from within a MultiActionController?
Thanks,
Todd
new method signature
/**
* This methods loads data from the id and
*
* @param request
* @param response
* @param errors
* @param vc
* @return
*/
public ModelAndView edit(HttpServletRequest request,
HttpServletResponse response, BindException errors) ;
Jörg Heinicke
Jun 20th, 2007, 05:43 PM
However I'm extending a MultiActionController. When I try to use the following method signature, spring blows up. BindException extends exception, how can I get to the errors object to get my model map from within a MultiActionController?
Unfortunately, MultiActionController works completely different as I found out recently (http://forum.springframework.org/showthread.php?t=39374). Please try the solution pointed out there. Feel free to ask if you have problems with it or questions in general.
Jörg
tnine
Jul 7th, 2007, 12:32 AM
Sorry for the late reply, I've been on vacation. I've overridden the initBinder method, and it never seems to get called, this is only a recent development. I'm not overriding it anywhere, so this code should get called, and its not. Any ideas?
/*
* (non-Javadoc)
*
* @see org.springframework.web.servlet.mvc.multiaction.Mu ltiActionController#initBinder(javax.servlet.http. HttpServletRequest,
* org.springframework.web.bind.ServletRequestDataBin der)
*/
@Override
protected void initBinder(HttpServletRequest request,
ServletRequestDataBinder binder) throws Exception {
System.err.println("In init binder!");
binder.registerCustomEditor(Date.class, DATE_EDITOR);
request.setAttribute(BINDER, binder);
super.initBinder(request, binder);
}
Jörg Heinicke
Jul 12th, 2007, 11:10 PM
initBinder() only gets called when there is something to bind to. Actually you need your method to have more than two parameters with the last one being the command object to bind to. See the code (http://springframework.cvs.sourceforge.net/springframework/spring/src/org/springframework/web/servlet/mvc/multiaction/MultiActionController.java?revision=1.33.2.1&view=markup) for details (line 400 ff.). There bind() gets called which calls initBinder().
Jörg
tnine
Jul 13th, 2007, 02:53 AM
Right, I understand that, but how can I get the date formatter to apply to a form loaded from a GET, not a POST? I use the following link to edit company information in a table.
company/edit?id=1
As you can see, the parameter is "id" and the value is equal to 1, since this is a simple query string initBinder won't get called, only the "edit" method. As a result, my dates always are displayed incorrectly since my bean contains a java.util.Date object. It seems the only solution is to use the date format tag suggested previously, but this seems like duplicate work that the spring tags should take care of. Is there some sort of configuration I can use in the application context that will apply a date formatter to all dates regardless of page or controller?
Jörg Heinicke
Jul 14th, 2007, 02:40 AM
but how can I get the date formatter to apply to a form loaded from a GET, not a POST?
From what I see MAC has no difference for GET and POST.
since this is a simple query string initBinder won't get called, only the "edit" method.
I can't follow this reasoning. In how far determines the "simple query" whether initBinder will be called or not?
Jörg
tnine
Jul 14th, 2007, 04:58 PM
I didn't use the best terminology in my previous post, but basically, if its not a form submission, initBinder never gets called, as such I can't use it to bind my date formatter to a display that isn't from a form submit.
Jörg Heinicke
Jul 14th, 2007, 05:41 PM
That's what I got from your post. But MAC seems to make no difference on form submission or not. That's why I did not understand your reasoning. There is no code like "if isFormSubmission() then call initBinder()" in MAC.
Jörg
tnine
Jul 15th, 2007, 04:20 PM
Any idea's why it doesn't call initBinder then?
Jörg Heinicke
Jul 15th, 2007, 07:37 PM
Unfortunately I have no idea. That's were I would start to debug remotely.
Jörg
kmoore4now
Dec 5th, 2007, 09:48 PM
I have experienced a very similar problem that can be duplicated in the PetClinic example that ships with Spring 2.5. See //http://forum.springframework.org/showthread.php?t=47120
The formatting only seems to work if the model attribute is made a session attribute.
vBulletin® v3.7.3, Copyright ©2000-2008, Jelsoft Enterprises Ltd.