Grails请求参数类型转换

Dón*_*nal 3 grails spring-mvc

在我的Grails应用程序中,我需要将请求参数绑定到Date命令对象的字段.为了执行所述字符串到日期转换,需要注册的适当属性编辑grails-app\conf\spring\resources.groovy

我添加了以下bean定义:

import org.springframework.beans.propertyeditors.CustomDateEditor
import java.text.SimpleDateFormat

    beans = {
        paramDateEditor(CustomDateEditor, new SimpleDateFormat("dd/MM/yy"), true) {} 
    }
Run Code Online (Sandbox Code Playgroud)

但我仍然收到一个错误:

java.lang.IllegalArgumentException: Could not parse date: Unparseable date: "04/01/99"]
Run Code Online (Sandbox Code Playgroud)

我认为我定义bean的方式可能有些不对劲,但我不知道是什么?

Joh*_*ner 8

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

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

beans = { 
    customPropertyEditorRegistrar(util.CustomPropertyEditorRegistrar) 
}
Run Code Online (Sandbox Code Playgroud)

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)); 
  } 
} 
Run Code Online (Sandbox Code Playgroud)