简体   繁体   English

Grails将命令对象添加到版本会导致params的id和版本不被绑定

[英]Grails adding version to command object causes id and version from params not to be bound

I apologize if I'm missing something really obvious here but I've been pulling my hair out with this issue. 如果我错过了一些非常明显的东西,我很抱歉,但是我一直在用这个问题解决问题。

I have a command object: 我有一个命令对象:

class MyCommand {
    Long id
    String value
}

I bind to this in my controller: 我在我的控制器中绑定到这个:

public update(MyCommand myCmd) {


}

Everything is fine in this scenario. 在这种情况下,一切都很好。 Now I'm trying to add the version field, which is passed in the request parameters to the command object: 现在我正在尝试添加版本字段,该字段在请求参数中传递给命令对象:

class MyCommand {
    Long id
    Long version
    String value
}

Now however when the binding happens the id and version are always null, even though they are present in the params object. 现在,当绑定发生时,id和version始终为null,即使它们存在于params对象中。

I suspected that there may be some special handling for id / version attributes related to how grails handles optimistic locking (as this is ultimately why I'm doing this) but the issue is present at the command object independent of any domain object. 我怀疑对于与grails如何处理乐观锁定相关的id / version属性可能有一些特殊处理(因为这最终是我这样做的原因)但是问题出现在命令对象中,与任何域对象无关。

I'm baffled why this is not working. 我很困惑,为什么这不起作用。 Is there some special case when version is present on a command object? 在命令对象上存在版本时是否存在某些特殊情况?

Seems this is by design per Jeff Brown jira 似乎这是根据Jeff Brown jira设计的

The data binding explicitly avoids binding id or version [if] they both exist and does this by design. 数据绑定显式避免绑定id或版本[if]它们都存在并按设计进行。 This is a shield against potential security problems relevant to data binding as it relates to domain classes. 这是针对与域类相关的数据绑定相关的潜在安全问题的屏障。 A simple work around for command objects would be to name the properties with something like "idValue" and "versionValue" or anything other than "id" and "version". 对命令对象进行简单的解决方法是使用类似“idValue”和“versionValue”或“id”和“version”以外的任何名称来命名属性。

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

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