在 Grails 控制器中使用 Groovy 特征
Use a Groovy trait in a Grails controller
我想按照以下方式在 Grails 控制器中使用 Groovy 特性:
trait ColumnSelectionController {
def selectColumns() {
//Do something here
}
}
class MyController implements ColumnSelectionController {
def index() {
//Calculate list model
}
}
但是,当我在 Grails 中 运行 执行此操作时,"selectColumns" 操作不可见,并且我从 Grails 收到 404 响应。我怀疑我需要对特征做一些事情,以便在其内部定义的方法被识别为实现 class 中的操作。有人知道那可能是什么吗?
编辑 1:
更多信息:特征是在 src/groovy 中定义的,而不是在 grails-app/controllers 中定义的,因此它没有被定义为人工制品。
编辑 2:
此外,如果我将特征更改为 class,用 @Artefact 注释标记它并更改 MyController 以扩展此 class,一切都按预期工作。尝试在特征上使用 @Artefact 注释没有任何作用(不足为奇)。
只需在 trait 中定义的方法上定义 @Action 注释,这将使该方法作为控制器的 Action(当实现 traits 时)
import grails.web.Action
trait ColumnSelectionController {
@Action
def selectColumns() {
//Do something here
}
}
希望对您有所帮助。
我想按照以下方式在 Grails 控制器中使用 Groovy 特性:
trait ColumnSelectionController {
def selectColumns() {
//Do something here
}
}
class MyController implements ColumnSelectionController {
def index() {
//Calculate list model
}
}
但是,当我在 Grails 中 运行 执行此操作时,"selectColumns" 操作不可见,并且我从 Grails 收到 404 响应。我怀疑我需要对特征做一些事情,以便在其内部定义的方法被识别为实现 class 中的操作。有人知道那可能是什么吗?
编辑 1:
更多信息:特征是在 src/groovy 中定义的,而不是在 grails-app/controllers 中定义的,因此它没有被定义为人工制品。
编辑 2:
此外,如果我将特征更改为 class,用 @Artefact 注释标记它并更改 MyController 以扩展此 class,一切都按预期工作。尝试在特征上使用 @Artefact 注释没有任何作用(不足为奇)。
只需在 trait 中定义的方法上定义 @Action 注释,这将使该方法作为控制器的 Action(当实现 traits 时)
import grails.web.Action
trait ColumnSelectionController {
@Action
def selectColumns() {
//Do something here
}
}
希望对您有所帮助。