gpt4 book ai didi

grails - 请求参数未绑定(bind)到 grails 命令对象

转载 作者:行者123 更新时间:2023-12-02 15:34:42 25 4
gpt4 key购买 nike

我已经从 Grails 1.3.6 升级到使用 Grails 2.1.1,并且一些正在工作的命令对象不再从请求参数中获取数据绑定(bind)。

我创建了一个实验 Controller 来重现该问题:

package my.controllers

import troubleshooting.*

class ExperimentalController {


def toggle(ExperimentalCommand cmd){
render "<pre>${this.properties}</pre>"
render "<h3>Raw</h3>The cmd.id was/is '${cmd.id}'<br/>The params.id was/is '${params.id}'<br/>They should be the same but they aren't<br/>The target ala cmd then params was/is '${cmd.target}' '${params.target}'<br/><hr/><hr/>"
bindData(cmd,params)//not even this works wtf?
render "<h3>bindData(cmd,params)</h3>The cmd.id was/is '${cmd.id}'<br/>The params.id was/is '${params.id}'<br/>They should be the same but they aren't<br/>The target ala cmd then params was/is '${cmd.target}' '${params.target}'<br/><hr/><hr/>"
cmd = new ExperimentalCommand()
render "<h3>New Cmd</h3>The cmd.id was/is '${cmd.id}'<br/>The params.id was/is '${params.id}'<br/>They should be the same but they aren't<br/>The target ala cmd then params was/is '${cmd.target}' '${params.target}'<br/><hr/><hr/>"
cmd = new ExperimentalCommand(params)
render "<h3>New Cmd binding constructor</h3>The cmd.id was/is '${cmd.id}'<br/>The params.id was/is '${params.id}'<br/>They should be the same but they aren't<br/>The target ala cmd then params was/is '${cmd.target}' '${params.target}'<br/><hr/><hr/>"
bindData(cmd,params,[include:["id","target"]])
render "The cmd.id was/is '${cmd.id}'<br/>The params.id was/is '${params.id}'<br/>They should be the same but they aren't<br/>The target ala cmd then params was/is '${cmd.target}' '${params.target}'<br/><hr/><hr/>"

}
}
class ExperimentalCommand{
def id,target,action,controller

}

如果你执行:

grails 运行应用程序

有了它,然后转到:
http://localhost:8080/YourApp/experimental/toggle/foo?target=bullseye&cmd.target=whatever

您会看到(在我的情况下)第一次尝试渲染 cmd.id 将其显示为 null,尽管 params.id 是 foo

从这个练习中,我还发现未指定包含 id 和目标的 bindData 调用完全失败,并且使用参数显式实例化命令对象会导致异常。

我在这里完全不知所措。我已经尝试过诸如覆盖 getInstanceControllersApi 方法以返回一个包装器,该包装器记录不同的 bindData 调用,这样我就可以看到发生了什么以及如何控制它,但什么也没告诉我。

我可以将 Controller 和操作字段添加到命令对象,以防止它在我调用 new ExperimentalCommand(params) 时出错,但我不应该做任何 Grails 文档声明绑定(bind)将在实例上完成的事情调用操作之前的命令对象,但在我的情况下它不是。

谷歌没有发现任何东西,所以显然我是第一个必须处理这个问题的人之一。

问题是我该如何解决这个问题?

最佳答案

在 1.3.6 和 2.x 之间,数据绑定(bind)机制发生了各种变化,我首先要尝试为命令对象属性赋予正确的类型,而不仅仅是 def

class ExperimentalCommand{
String id
String target
String action
String controller
}

The Grails 2.1.1 docs数据绑定(bind)谈谈 new CommandObject(params)obj.properties = params数据绑定(bind)的形式和状态

These forms of data binding in Grails are very convenient, but also indiscriminate. In other words, they will bind all non-transient, typed [my bold] instance properties of the target object, including ones that you may not want bound.

关于grails - 请求参数未绑定(bind)到 grails 命令对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13477733/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com