gpt4 book ai didi

Grails 命令对象初始化

转载 作者:行者123 更新时间:2023-12-02 15:00:48 24 4
gpt4 key购买 nike

在我的 Grails 2.3.8 应用程序中,我定义了以下 Controller 操作

class RegisterController {

def register(User user) {
render text: "User name is '$user.name'"
}
}
user参数是域类实例。如果我用 URL 调用这个 Controller
http://localhost:8080/myapp/register/register

我得到一个 NullPointerException .但是我对数据绑定(bind)的理解是,如果在没有任何参数的情况下调用此操作,则应为参数分配 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()



情况不一定如此。对于域类命令对象,如果不存在参数,则仅为 POST 请求创建一个新实例。

来自 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.



2.3.8 文档中可能缺少该文本。我将验证并在必要时添加它。

关于Grails 命令对象初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23569875/

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