在我的Grails应用程序中,我需要将请求参数绑定到命令对象的Date
字段。为了执行String-to-Date转换,需要在grails-app\conf\spring\resources.groovy
中注册适当的PropertyEditor
我添加了以下bean定义:
import org.springframework.beans.propertyeditors.CustomDateEditor
import java.text.SimpleDateFormat
beans = {
paramDateEditor(CustomDateEditor, new SimpleDateFormat("dd/MM/yy"), true) {}
}
但我仍然收到错误:
java.lang.IllegalArgumentException: Could not parse date: Unparseable date: "04/01/99"]
我认为我定义bean的方式可能有些不对劲,但我不知道是什么?
答案 0 :(得分:8)
您缺少的是注册新的属性编辑器。当我升级到Grails 1.1并且必须以MM / dd / yyyy格式绑定日期时,以下内容对我有用。
beans = {
customPropertyEditorRegistrar(util.CustomPropertyEditorRegistrar)
}
package util
import java.util.Date
import java.text.SimpleDateFormat
import org.springframework.beans.propertyeditors.CustomDateEditor
import org.springframework.beans.PropertyEditorRegistrar
import org.springframework.beans.PropertyEditorRegistry
public class CustomPropertyEditorRegistrar implements PropertyEditorRegistrar {
public void registerCustomEditors(PropertyEditorRegistry registry) {
registry.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("dd/MM/yy"), true));
}
}