在grails 2.4.x中有一个名为src/templates/scaffolding/Controller.groovy
的模板,用于为动态脚手架生成CRUD操作,如:
class BookController {
static scaffold = domain.Book
static responseFormats = ['html', 'json']
}
在Grails 3(3.1.x)中,交替使用此文件(可能是3.0.x install-templates
为我创建了一个)只是不起作用。有没有办法改变grails 3(.1)中动态脚手架的默认行为? install-templates
创建了一些src/main/templates/scaffolding/ScaffoldedController.groovy
文件,但其内容类似于我的BookController
类。文档没有说明这一点。
答案 0 :(得分:1)
查看create-script命令。
这将允许您创建自己的脚本来进行代码生成,但我不相信它允许您访问Grails 3中的GORM域属性。(1)。我实际上在这里发布了这个问题:How can I access GORM object properties in a GroovyScriptCommand?
我首先运行install-templates命令然后运行create-script命令,不确定是否需要,但是它给了我一些有限的示例模板来查看。
这是我创建的一个例子。我把println语句放入,以便我可以看到模型的不同属性是我必须使用的。他们感觉有点受限,因为它们都是基于您输入的命令行参数而不是实际Grails工件的实例。
的src /主/脚本/木屐创建屏幕-常规:
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:... ${model.packageName}"
println "DAC: model.simpleName:.... ${model.simpleName}"
println "DAC: model.lowerCaseName:. ${model.lowerCaseName}"
println "DAC: model.packagePath:... ${model.packagePath}"
render template: template('scaffolding/EtaController.groovy'),
destination: file("grails-app/controllers/${model.packagePath}/${model.convention("Controller")}.groovy"),
model: model,
overwrite: overwrite
render template: template('scaffolding/EtaDomainObject.groovy'),
destination: file("grails-app/domain/${model.packagePath}/${model.convention("Domain")}.groovy"),
model: model,
overwrite: overwrite
render template: template('scaffolding/EtaService.groovy'),
destination: file("grails-app/services/${model.packagePath}/${model.convention("Service")}.groovy"),
model: model,
overwrite: overwrite
render template: template('scaffolding/EtaGsp.gsp'),
destination: file("grails-app/views/${model.packagePath}/${model.propertyName}/${model.propertyName}.gsp"),
model: model,
overwrite: overwrite
render template: template('scaffolding/EtaGspTabHeader.gsp'),
destination: file("grails-app/views/${model.packagePath}/${model.propertyName}/_tabHeader.gsp"),
model: model,
overwrite: overwrite
return true
模板: 的src /主/模板/脚手架/ EtaController.groovy
<%=packageName ? "package ${packageName}" : ''%>
class ${className}Controller {
static scaffold = ${className}
def index (){
render view: "${packageName.replaceAll('\\\\', '/')}/${propertyName}/${propertyName}.gsp"
}
}
执行命令: grails geta-create-screen my.package.MyClass --force --stacktrace --verbose
答案 1 :(得分:0)
我认为您正在寻找generate-controller
。