mkr February 2016

Grails3 controller template for dynamic scaffolding

In grails 2.4.x there was template named src/templates/scaffolding/Controller.groovy which was used to generate CRUD actions for dynamic scaffolding like:

class BookController {
        static scaffold = domain.Book
        static responseFormats = ['html', 'json']
}

In grails 3 (3.1.x) alternating this file (probably 3.0.x install-templates created one for me) just does not work. Is there any way to alter default behavior of dynamic scaffolding in grails 3(.1)? install-templates creates some src/main/templates/scaffolding/ScaffoldedController.groovy file but its content looks like my BookController class. Documentation says nothing about that.

Answers


Sascha Frinken February 2016

I think you are looking for generate-controller.


DAC February 2016

Take a look at the create-script command.

create-script quick ref...

create-script User Guide

That will allow you to create your own script to do code generation, but I do not believe it allows you to get access to GORM domain properties in Grails 3.(1). I actually posted that question my self here: How can I access GORM object properties in a GroovyScriptCommand?

I first ran the install-templates command and then the create-script command, not sure if needed to, but it gave me some limited example templates to look at.

Here's an example of the one I created. I put the println statements in just so that I could see what the different properties on the model are that I have to work with. They feel a little limited because they are all based off of the command line argument that you enter and not an instance of an actual Grails artifact.

src/main/scripts/geta-create-screen-groovy:

import grails.build.logging.ConsoleLogger

description("Creates a GETA scaffolded controller, views, and integration test artifacts") {
    usage 'geta-create-screen [domain name]'
    completer org.grails.cli.interactive.completers.DomainClassCompleter
    argument name:'Controller Name', description:"The name of controller", required:true
    flag name:'force', description:"Whether to overwrite existing files"
}

def model = model(args[0])
def overwrite = flag('force') ? true : false

println "DAC: model.className:..... ${model.className}"
println "DAC: model.fullName:...... ${model.fullName}"
println "DAC: model.propertyName:.. ${model.propertyName}"
println "DAC: model.packageName: 

Post Status

Asked in February 2016
Viewed 3,566 times
Voted 6
Answered 2 times

Search




Leave an answer