Grails parameter type conversion

In my Grails application, I need to bind the request parameter to the field Dateof the command object. To perform a String-to-Date conversion, you must register the corresponding PropertyEditor ingrails-app\conf\spring\resources.groovy

I added the following bean definiton:

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

    beans = {
        paramDateEditor(CustomDateEditor, new SimpleDateFormat("dd/MM/yy"), true) {} 
    }

But I still get the error message:

java.lang.IllegalArgumentException: Could not parse date: Unparseable date: "04/01/99"]

I think that maybe something is wrong with how I defined the bean, but I have no idea what?

+3
source share
1 answer

You are missing the partial registration of a new property editor. The following worked for me when I upgraded to Grails 1.1 and I had to bind dates in MM / dd / yyyy format.

Grails app / Config / spring / resources.groovy:

beans = { 
    customPropertyEditorRegistrar(util.CustomPropertyEditorRegistrar) 
}

/ groovy/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)); 
  } 
} 
+8

Source: https://habr.com/ru/post/1716730/


All Articles