[英]Grails 3: Binding multiple command objects via POST
I have a controller with a method that I want to bind multiple command objects too.我有一个 controller 的方法,我也想绑定多个命令对象。 When I call the method via GET, it works great and both objects get bound.
当我通过 GET 调用方法时,效果很好,两个对象都被绑定了。 The issue is if I call the method via POST, only the first command object gets bound and the second one is completely ignored.
问题是如果我通过 POST 调用方法,只有第一个命令 object 被绑定,第二个命令被完全忽略。
Simple example:简单示例:
def register(MembershipCommand cmd1, RegisterCommand cmd2) {
println(cmd1.email);
println(cmd2.pass);
respond([:]);
}
If I call /register?email=test&pass=test
then cmd1
and cmd2
get populated如果我调用
/register?email=test&pass=test
然后cmd1
和cmd2
得到填充
If I call /register
with POST data {email:test,pass:test}
, cmd1
gets populated and cmd2.pass
is null
.如果我使用 POST 数据调用
/register
{email:test,pass:test}
, cmd1
会被填充并且cmd2.pass
是null
。
Is there a way to make this data binding work using POST?有没有办法使用 POST 使这个数据绑定工作? I can't really use GET because file uploads are involved and plus the forms are fairly large.
我不能真正使用 GET,因为涉及文件上传,而且 forms 相当大。
I know that another option would be to just split the method into 2, 1 for each object and have my form submit to each separately but I want to avoid that if I can.我知道另一种选择是将方法分成 2 个,每个 object 1 个,然后让我的表单分别提交给每个表单,但我想尽可能避免这种情况。
Any ideas?有任何想法吗?
The solution to get POST working was to restructure my form data into an object style format.使 POST 工作的解决方案是将我的表单数据重组为 object 样式格式。
So instead of {email:test,pass:test}
所以而不是
{email:test,pass:test}
I would have {cmd1:{email:test}, cmd2:{pass:test}}
我会有
{cmd1:{email:test}, cmd2:{pass:test}}
I have created a minimal working project to test your idea.我创建了一个最小的工作项目来测试你的想法。 It works like a charm.
它就像一个魅力。 Below is the snippets.
以下是片段。
RegisterCmd.groovy寄存器Cmd.groovy
class RegisterCmd {
String email
}
edit.gsp编辑.gsp
<g:form resource="${this.player}" method="POST" action="customisedUpdate">
<g:hiddenField name="version" value="${this.player?.version}" />
<fieldset class="form">
<f:field bean="player" property="name" />
<f:field bean="player" property="game" />
<f:field bean="player" property="region" />
<label>Email</label><g:field type="text" name="email"/>
</fieldset>
<fieldset class="buttons">
<input class="save" type="submit" value="${message(code: 'default.button.update.label', default: 'Update')}" />
</fieldset>
</g:form>
PlayerController.groovy PlayerController.groovy
@Transactional
def customisedUpdate(Player player, RegisterCmd registerCmd) {
println "Calling save ${player.dump()}"
println "RegisterCmd: ${registerCmd.dump()}"
//end::save[]
//tag::save-handleErrors[]
if (player == null) {
render status: HttpStatus.NOT_FOUND
return
}
if (player.hasErrors()) {
respond player.errors, view: 'create'
return
}
//end::save-handleErrors[]
player.save flush: true
request.withFormat {
form multipartForm { redirect player }
'*' { respond player, status: HttpStatus.CREATED }
}
//tag::save[]
}
The output looks like: output 看起来像:
Calling save <com.itersdesktop.javatechs.grails.Player@1c25113d name=Alexis Barnett game=Pandemic region=EAST wins=96 losses=30 id=1 version=4 org_grails_datastore_mapping_dirty_checking_DirtyCheckable_
_$changedProperties=[name:HUE THI MY NGO] org_grails_datastore_gorm_GormValidateable__errors=org.grails.datastore.mapping.validation.ValidationErrors: 0 errors org_grails_datastore_gorm_GormValidateable
__skipValidate=false>
RegisterCmd: <com.itersdesktop.javatechs.grails.RegisterCmd@7ee6bb8c email=alexis.barnett@gmail.com grails_validation_Validateable__beforeValidateHelper=org.grails.datastore.gorm.support.BeforeValidateH
elper@3409999e grails_validation_Validateable__errors=grails.validation.ValidationErrors: 0 errors>
If you are interested in the project, please consult it at https://bitbucket.org/itersdesktop/command-objects/src/master/如果您对该项目感兴趣,请在https://bitbucket.org/itersdesktop/command-objects/src/master/咨询
For Grails 3.3.X having multiple command objects doesn't seem to be supported.对于 Grails 3.3.X 似乎不支持具有多个命令对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.