简体   繁体   English

Grails - 将参数绑定到另一个名称的命令对象字段

[英]Grails - Bind parameter to command object field of a different name

If I have a command object SomeClassCommand with a String field someField but want to bind data from a parameter params.otherField, how do I go about doing that? 如果我有一个带有字段someField的命令对象SomeClassCommand但想要绑定参数params.otherField中的数据,我该如何去做呢? Is there an annotation I can put in the command object? 我可以在命令对象中添加注释吗?

Actually there is a horrendous work around which defies the purpose of auto binding in your case. 实际上,在你的情况下,有一个可怕的工作违背了自动绑定的目的。

def map = [:]
map.someField = params.otherField
//plus set all the other params to map
map << params

def commandObj = new SomeCommandObj()

//Explicitly bind map to command object
bindData(commandObj, map)

It really is horrendous, because you are doing extra work only to bind data. 它真的很可怕,因为你只是为了绑定数据而做额外的工作。 You could have directly set values to Command Object. 您可以直接将值设置为Command Object。

I would suggest either to change the command object field name or the parameter field name, which ever is controllable. 我建议更改命令对象字段名称或参数字段名称,这是可控制的。 AFAIK there is no annotation available unless you have your own utility to do such. 除非您有自己的实用程序,否则AFAIK没有可用的注释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM