开发者

Does Grails have a neat way of copy domain properties from a URL query string?

开发者 https://www.devze.com 2022-12-17 19:20 出处:网络
I know Grails has a map based constructor for domain objects, to which you can pass the params of a URL to and it will apply the appropriate field settings to the object using introspection, like this

I know Grails has a map based constructor for domain objects, to which you can pass the params of a URL to and it will apply the appropriate field settings to the object using introspection, like this...

myDomainInstance = new MyObject(params)

I was wondering whether there was an equivalent method of taking the params and applying them to an existing object and updating values in the same way that the map constructor must work, something like...

myDomainInstance = params

or

myDomainInstance = fromParams(params)

Am I just wishful thinking or does such a thing exist? I can code it up myself but would 开发者_JS百科rather not if it exists already.

Thanks


Adapted from the grails user guide:

obj = MyObject.get(1)
obj.properties = params

Check out the documentation for 'params' under the controller section for more information.


It really depends on what you are trying to do but an equivalent approach use databinding.

def sc = new SaveCommand()
bindData(sc, params)

This give you the benefit of using custom binding. If let say your date format is not the default one you can redefine it through a bean like this:

public class CustomPropertyEditorRegistrar implements PropertyEditorRegistrar {
  public void registerCustomEditors(PropertyEditorRegistry registry) {
      registry.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("dd/MM/yyyy"), true));
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消