[英]Grails command object initialization
In my Grails 2.3.8 app, I've defined the following controller action 在我的Grails 2.3.8应用中,我定义了以下控制器操作
class RegisterController {
def register(User user) {
render text: "User name is '$user.name'"
}
}
The user
argument is a domain class instance. user
参数是域类实例。 If I invoke this controller with the URL 如果我使用URL调用此控制器
http://localhost:8080/myapp/register/register
I get a NullPointerException
. 我得到了
NullPointerException
。 However my understanding of databinding is that if this action is invoked without any parameters, the argument should be assigned a new User()
但是我对数据绑定的理解是,如果在没有任何参数的情况下调用此操作,则应为该参数分配一个
new User()
However my understanding of databinding is that if this action is invoked without any parameters, the argument should be assigned a new User()
但是我对数据绑定的理解是,如果在没有任何参数的情况下调用此操作,则应为该参数分配一个新的User()
That is not necessarily the case. 不一定是这样。 For domain class command objects if no parameters are present a new instance is only created for POST requests.
对于域类命令对象,如果不存在任何参数,则仅为POST请求创建一个新实例。
From http://grails.org/doc/2.4.0.RC1/guide/theWebLayer.html#commandObjects ... 从http://grails.org/doc/2.4.0.RC1/guide/theWebLayer.html#commandObjects ...
If the command object's type is a domain class and there is no id request parameter then null will be passed into the controller action unless the HTTP request method is "POST", in which case a new instance of the domain class will be created by invoking the domain class constructor.
如果命令对象的类型是域类,并且没有id请求参数,则将null传递到控制器操作中,除非HTTP请求方法为“ POST”,在这种情况下,将通过调用来创建域类的新实例域类的构造函数。
That text may be missing from the 2.3.8 docs. 2.3.8文档中可能缺少该文本。 I will verify that and add it if necessary.
我将对此进行验证,并在必要时添加它。
What if you modify: 如果您修改:
'$user.name'
To be: 成为:
'${user?.name}'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.