Grails request parameter type conversion

后端 未结 1 1714

In my Grails app, I need to bind a request parameter to a Date field of a command object. In order to perform the String-to-Date conversion, one needs to regist

1条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-06 19:49

    The piece you are missing is registering of the new property editor. The following worked for me when I upgraded to Grails 1.1 and had to bind dates in the MM/dd/yyyy format.

    grails-app/config/spring/resources.groovy:

    beans = { 
        customPropertyEditorRegistrar(util.CustomPropertyEditorRegistrar) 
    }
    

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

    0 讨论(0)
提交回复
热议问题