简体   繁体   English

请求参数未绑定到Grails命令对象

[英]Request params not getting bound to grails command object

I've upgraded to use Grails 2.1.1 from Grails 1.3.6 and some command objects which were working are no longer getting data binding from the request parameters. 我已经从Grails 1.3.6升级为使用Grails 2.1.1,并且某些正在运行的命令对象不再从请求参数获取数据绑定。

I created an experimental controller to reproduce the problem: 我创建了一个实验控制器来重现该问题:

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

}

If you execute: 如果执行:

grails run-app Grails运行应用程序

With that and then go to: 有了这个,然后去:

http://localhost:8080/YourApp/experimental/toggle/foo?target=bullseye&cmd.target=whatever

You'll see (under my circumstances) that the first attempt to render the cmd.id shows it as null despite the fact that the params.id is foo 您将看到(在我的情况下)尽管params.id为foo,但首次尝试渲染cmd.id时仍将其显示为null。

From this excercise I've also discovered that the bindData calls that don't specify include id and target exclusively fail AND instantiating the command object explicitly with the params causes an exception. 从这个练习中,我还发现,未指定bindData和目标的bindData调用完全失败,并且使用params显式实例化命令对象会导致异常。

I'm at a complete loss here. 我在这里完全不知所措。 I've tried things like overriding the getInstanceControllersApi method to return a wrapper that logs the different bindData calls so I can see what happens and how I might controll it only that told me nothing. 我已经尝试过诸如重写getInstanceControllersApi方法以返回记录不同的bindData调用的包装器之类的事情,以便我可以看到发生了什么以及如何控制它,这只会使我什么都没有。

I can add controller and action fields to the command object to prevent it from erroring out when I call new ExperimentalCommand(params) but I shouldn't have to do any of that the Grails documentation states that the binding will get done on the instance of the command object before the action is called however in my case it is not. 我可以在命令对象中添加控制器和操作字段,以防止在调用新的ExperimentCommand(params)时出错,但是我不必执行任何Grails文档指出的绑定操作在操作之前调用的命令对象,但是在我看来不是。

Google hasn't turned up anything so apparently I am one of the first to have to deal with this. Google尚未提供任何服务,因此我显然是最早处理此问题的人之一。

The question is HOW can I trouble shoot this? 问题是我该如何解决这个问题?

There have been various changes to the data binding mechanism between 1.3.6 and 2.x, the first thing I would try is to give the command object properties proper types rather than just def 1.3.6和2.x之间的数据绑定机制发生了各种变化,我尝试做的第一件事是为命令对象属性提供适当的类型,而不仅仅是def

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

The Grails 2.1.1 docs for data binding talk about the new CommandObject(params) and obj.properties = params forms of data binding and state that 用于数据绑定的Grails 2.1.1文档讨论了new CommandObject(params)obj.properties = params数据绑定形式,并指出

These forms of data binding in Grails are very convenient, but also indiscriminate. Grails中的这些形式的数据绑定非常方便,但也是不加区别的。 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. 换句话说,它们将绑定目标对象的所有非瞬态类型 [我的粗体]实例属性,包括您可能不想绑定的属性。

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

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