Grails3 generate-all 生成错误的创建 Action 代码

标签 grails data-binding scaffolding

当我使用 generate-all package.DomainObject 时,它会生成一个 Controller ,其中创建操作生成为:

def create() {

respond new DomainObject(params)   

}


当我调用 localhost:8080/DomainObject/create 时,即使没有进行任何代码更改,它也会引发异常:

groovy.lang.MissingPropertyException: No such property: controller for class: package.DomainObject


对于 params map 具有而 DomainObject 没有的属性,内省(introspection)似乎失败了。这很令人惊讶,因为在 grails 2 中,自省(introspection)过去常常忽略不匹配的属性,并且它还用于对字段进行必要的类型转换(现在如果 DomainObject 具有 int 属性,它将抛出类型不匹配异常,因为 params map 将其作为字符串传递)。这真的很不方便。有什么改变还是我错过了什么?

最佳答案

在 Grails 中使用映射构造函数和批量设置属性与在 Groovy 中的映射基本相同,但它具有排除“ Controller ”、“ Action ”和“格式”键的逻辑,以保持 Controller 代码整洁。这在 3.x 和 has been reported in the issue tracker 中中断了.它没有标记为已修复,但在一个简单的 3.0.4 测试应用程序中对我来说可以正常工作。

作为临时解决方法,您可以复制参数映射并删除存储在这些键下的值,并为构造函数使用“固定”映射:

def create() {
   def fixedParams = ([:] + params) // copy
   ['controller', 'format', 'action'].each { fixedParams.remove it }
   respond new Thing(fixedParams)
}

关于Grails3 generate-all 生成错误的创建 Action 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32293579/

相关文章:

linux - 以编程方式重启 Grails 应用程序

grails - Geb-Spock使用了错误的Groovy版本

c# - 调试/断点为什么 WPF BindingExpression 为空

属性 app :itemIconTint will be ignored 的 Android 数据绑定(bind)应用程序命名空间

asp.net-mvc - MVCScaffolding 入门

c# - scafford 自动生成 crud 仓库 asp.net5

grails - 在 grails Controller 中使用私有(private)字段

unit-testing - 在域类上调用 .save() 失败,单元测试中没有方法 .save() 的签名

c# - 在较长的运行过程中禁用 WPF 按钮,MVVM 方式

Grails 2.5 脚手架在绑定(bind)期间不会删除最后一个 hasMany