Grails请求参数类型转换

时间:2009-09-02 21:32:13

标签: grails spring-mvc

在我的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的方式可能有些不对劲,但我不知道是什么?

1 个答案:

答案 0 :(得分:8)

您缺少的是注册新的属性编辑器。当我升级到Grails 1.1并且必须以MM / dd / yyyy格式绑定日期时,以下内容对我有用。

的grails-app /配置/弹簧/ resources.groovy:

beans = { 
    customPropertyEditorRegistrar(util.CustomPropertyEditorRegistrar) 
}

的src /常规/ UTIL / CustomPropertyEditorRegistrar.groovy:

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)); 
  } 
}