2

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 rather not if it exists already.

Thanks

4

2 に答える 2

7

grails ユーザーガイドから適応:

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

詳細については、コントローラー セクションの「params」のドキュメントを参照してください。

于 2010-01-20T16:52:01.410 に答える
2

それは実際に何をしようとしているのかによって異なりますが、同等のアプローチではデータバインディングを使用します。

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

これにより、カスタム バインドを使用する利点が得られます。日付形式がデフォルトのものではない場合、次のように Bean を介して再定義できます。

public class CustomPropertyEditorRegistrar implements PropertyEditorRegistrar {
  public void registerCustomEditors(PropertyEditorRegistry registry) {
      registry.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("dd/MM/yyyy"), true));
  }
}
于 2010-01-21T08:06:49.093 に答える