当我使用 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/