展示形式= GT当春天@InitBinder未调用;没有定义CustomEditors [英] Spring @InitBinder not invoked when showing form => CustomEditors not defined

查看:399
本文介绍了展示形式= GT当春天@InitBinder未调用;没有定义CustomEditors的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下(简化到骨头)控制器:

I have following (simplified to the bone) Controller:

@Controller  
public class TestController  {

@RequestMapping(value = "/test.htm", method = RequestMethod.GET)
public String showForm(final ModelMap map) {
    final TestFilter filter = new TestFilter();
    filter.setStartDate(new Date(System.currentTimeMillis()));
    map.addAttribute("reportPerResourceForm", filter);
    return "test";
}

@InitBinder
public void initBinder(final WebDataBinder binder) {
    binder.registerCustomEditor(Date.class, null, new CustomDateEditor(new SimpleDateFormat("dd/MM/yyyy"), true));
}

}

在JSP:

<form:form commandName="reportPerResourceForm" id="reportForm">
    <form:input path="startDate" />
</form:form>

这是我快速创建测试出一个问题,我曾与另一视图 - 控制器的控制器。正如你可以在控制器看到CustomeDateEditor定义。在我的实际控制此编辑器工作正常;当你在表单字段,例如11/01/2010进入这个是很好的转换成编辑日期;还回去的形式日期又被很好地时转换回一个字符串。

This is a controller I quickly created to test out an issue I had with another view-controller. As you can see in the Controller a CustomeDateEditor is defined. In my actual controller this editor is working fine; when you enter for instance 11/01/2010 in the form field this is nicely converted into a Date by the editor; also when going back to the form the Date was again nicely converted back to a String.

然而,当我(在的TestController)要设置窗体上的默认日期那么这只是得到一个显示与Date.toString()的表单字段,而不是使用CustomDateEditor.getAsText返回值的()!一些调试后,我了解到,当RequestMethod ==让我InitBinder方法不叫。这是正常的吗?

However, when I (as in TestController) want to set a default date on the form then this gets simply displayed a Date.toString() in the form field instead of using the returned value from CustomDateEditor.getAsText()! After some debugging I learned that my InitBinder method is not called when RequestMethod == GET. Is this normal?

我敢肯定,我可以不使用此解决方法

您的帮助,谢谢搜索
斯泰恩

I'm sure I could workaround this by not using

Thanks for your help,
Stijn

推荐答案

使用 @ModelAttribute 设置域名转发到页面之前。

use @ModelAttribute to setup domain before forwarding to page.

谨慎使用当你处理春天,它只会创建对象的一个​​新实例Spring上下文之外,您不能使用任何春能力(如Web绑定验证等等)。

carefully to use new when you deal with spring, it will just create a new instance of object outside spring context and you cannot use any of spring capability (such as web binding, validation, etc).

例如:

@RequestMapping(value = "/test.htm", method = RequestMethod.GET)
public String showForm(@ModelAttribute yourDomain, final ModelMap map)

和你的域名,你可以使用:

and at your domain you can use :

@DateTimeFormat(pattern="dd/MM/yyyy")
private Date balance = new Date(System.currentTimeMillis());

这篇关于展示形式= GT当春天@InitBinder未调用;没有定义CustomEditors的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆