简体   繁体   中英

Grails3 generate-all generates faulty create action code

When I use generate-all package.DomainObject, it generates a controller where create action is generated as:

def create() {

 respond new DomainObject(params) 

}

When I call the localhost:8080/DomainObject/create even without making any code change, it throws an exception:

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

It looks like introspection is failing for properties that params map has and DomainObject does not have. This is surprising because in the grails 2, introspection used to just ignore the non-matching properties and it also used to do necessary type conversions on fields as well (now if DomainObject has an int property, it will throw a type mismatch exception because params map passes it as String).
This is really inconvenient. Did something change or I am missing something?

Using the map constructor and setting properties in bulk with a map in Grails is basically the same as in Groovy, but it has logic to exclude 'controller', 'action', and 'format' keys to keep controller code like this uncluttered. That broke in 3.x and has been reported in the issue tracker . It's not marked fixed but works correctly for me in a simple 3.0.4 test app.

As a temporary workaround you can copy the params map and remove values stored under those keys and use the 'fixed' map for the constructor:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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