在Grails控制器中使用Groovy特征

Ste*_*ole 8 grails groovy

我想在Grails控制器中使用Groovy特性,如下所示:

trait ColumnSelectionController {
    def selectColumns() {
        //Do something here
    }
}

class MyController implements ColumnSelectionController {
    def index() {
        //Calculate list model
    }
}
Run Code Online (Sandbox Code Playgroud)

但是当我在Grails中运行它时,"selectColumns"操作不可见,我从Grails得到404响应.我怀疑我需要对特征做一些事情,以便在其中定义的方法被识别为实现类中的动作.谁知道那可能是什么?

编辑1:

更多信息:特征在src/groovy中定义,而不是在grails-app/controllers中定义,因此它没有被定义为Artefact.

编辑2:

此外,如果我将特征更改为类,使用@Artefact注释标记它并更改MyController以扩展此类,一切都按预期工作.试图在特征上使用@Artefact注释没有任何意义(没什么大惊喜).

小智 9

只需在trait中定义方法定义@Action注释,这将使此方法作为控制器的Action(当特征实现时)

import grails.web.Action

trait ColumnSelectionController {

  @Action
  def selectColumns() {
     //Do something here
  }
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.