简体   繁体   English

grails命令对象id字段的数据绑定

[英]grails command object data binding of id field

When using command objects it seems I do not get automatic binding of the id field 当使用命令对象时,似乎我没有得到id字段的自动绑定

class somethingCommand {

 int id
 String A
 String B

 // some methods here like Domain.get(id)

}

My A and B string get auto-magically data binded from the form properties but not id. 我的A和B字符串从表单属性中获取自动魔术数据,但不是id。 The other "hidden fields" of grails like version, dateCreated or lastUpdated also get binded correctly. grails的其他“隐藏字段”如version,dateCreated或lastUpdated也正确绑定。

My current patched solution is the following: I resort to defining another hidden id field in my form 我目前修补的解决方案如下:我在我的表单中定义另一个隐藏的id字段

<g:hiddenField name="blogId" value="${blog?.id}"/>

And rename id to blogId in the command obect and that works. 并在命令obect中将id重命名为blogId并且该方法有效。

This does not seem to be in line with the elegance of Grails. 这似乎与Grails的优雅不符。 What am I missing in the data binding rules of Command object vs controller? 我在Command对象与控制器的数据绑定规则中缺少什么?

Following up on this problem: 跟进这个问题:

I ran into the same problem: I had a command with an id parameter. 我遇到了同样的问题:我有一个带有id参数的命令。 When calling my controller on an action that used the command, all parameters were properly bound except the id . 在使用该命令的操作上调用我的控制器时,除了id之外,所有参数都被正确绑定。

It turned out that if you have a field called version in your command, the id field will not be assigned. 事实证明,如果您的命令中有一个名为version的字段,则不会分配id字段。

If you change the name of your version field for something else (ie. readVersion), then the ID will be mapped properly. 如果您为其他内容更改版本字段的名称(即readVersion),则ID将正确映射。

Hope that helps, 希望有所帮助,

Vincent Giguere Vincent Giguere

I have used this several times. 我好几次使用过这个。

The id parameter is bound to your command as any other field. id参数与任何其他字段绑定到您的命令。 There is no special behaviour on this particular field 此特定字段没有特殊行为

Now, if you are submitting a value for the id field that is incompatible with the type of your command's id field, then the field will not be bound. 现在,如果要为id字段提交与命令的id字段类型不兼容的值,则该字段不会被绑定。 You will not get a ClassCastException or anything of the sort. 不会得到ClassCastException或任何类型的东西。 You will just end up having a null value for the field. 您将最终得到该字段的空值。

I remember seeing something tricky about that: If you have an id in both your URL (ex. controller/action/id ) and in your form, then the id from the URL takes precedence. 我记得看到一些棘手的事情:如果你的URL(例如控制器/动作/ id) 你的表单都有一个id ,那么URL中的id优先。

So if your form has a proper hidden field for ID 因此,如果您的表单具有适当的ID隐藏字段

<field type="hidden" name="id" value="1"/>

but the action is somehow screwed up on your form 但是这个动作在某种程度上搞砸了你的表格

<g:form action="doSometing" id="some-incompatible-value">...</g:form>

What you would receive in the controller is: 你在控制器中收到的是:

params.id = "some-incompatible-value"

Which would make it impossible for grails to convert your id parameter to a long or an int, and your command object would have 这将使grails无法将您的id参数转换为long或int,并且您的命令对象将具有

command.id = null

So, my advice would be : start over again and rewrite your form from scratch and make sure that the value in you form, as you see it from your controller's params.id is compatible with your command's id field type. 因此,我的建议是:重新开始并从头开始重写您的表单,并确保您的表单中的值,正如您从控制器的params.id中看到的那样与您的命令的id字段类型兼容。

Let me know how it goes. 让我知道事情的后续。

Vincent Giguère VincentGiguère

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

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