C. *_*yer 2 grails scaffolding
我正在尝试在遗留数据库之上构建grails(2.1.0)应用程序.它有很多桌子,我非常喜欢只使用动态脚手架.问题是某些表有一个字符串作为主键,但src/templates/scaffolding/Controller.groovy中的模板代码例如show是
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}]
}
Run Code Online (Sandbox Code Playgroud)
对于字符串键,这似乎将字符串转换为null,并且get因错误而失败$Domain not found with id null.
如果我运行生成控制器并将签名更改为def show(String id),它将按预期工作.
那么,有没有办法在"动态脚手架时间"检查域类并相应地编写方法?
在控制器模板中,您有一个domainClass变量,使您可以访问GrailsDomainClass表示要为其生成控制器的类,因此您可以执行类似的操作(同样适用于edit,update和delete):
def show(${domainClass.identifier.type.name} id) {
Run Code Online (Sandbox Code Playgroud)
应该生成def show(java.lang.Long id)或def show(java.lang.String id)适当的.