Grails:如何根据域ID类型动态更改脚手架代码中的方法签名?

时间:2012-09-07 14:37:00

标签: grails scaffolding

我正在尝试在遗留数据库之上构建grails(2.1.0)应用程序。它有很多桌子,我非常喜欢只使用动态脚手架。问题是某些表有一个字符串作为主键,但是src / templates / scaffolding / Controller.groovy中的模板代码用于例如秀是

def show(Long id) {
    def ${propertyName} = ${className}.get(id)
    if (!${propertyName}) {
        flash.message = message(code: 'default.not.found.message', args: [message(code: '${domainClass.propertyName}.label', default: '${className}'), id])
        redirect(action: "list")
        return
    }

    [${propertyName}: ${propertyName}]
}

对于字符串键,这似乎将字符串变为null,并且get失败并显示错误$Domain not found with id null

如果我运行生成控制器并将签名更改为def show(String id),它将按预期工作。

那么,有没有办法在“动态脚手架时间”检查域类并相应地编写方法?

1 个答案:

答案 0 :(得分:6)

在控制器模板中,您有一个domainClass变量,可以访问代表您要为其生成控制器的类的GrailsDomainClass,因此您可以执行类似的操作(同样适用于{ {1}},editupdate):

delete

应根据需要生成def show(${domainClass.identifier.type.name} id) { def show(java.lang.Long id)

相关问题