Grails:如何通过过滤器中的controllerName获取控制器?

Grails:如何通过过滤器中的controllerName获取控制器?,grails,filter,Grails,Filter,我有一个过滤器和controllerNamevar获取控制器目标 例如: 当用户尝试访问/myApp/book/index时,我的过滤器被触发,controllerName等于book。如何获取BookController实例 Tks 编辑: 我可以使用以下方法获得人工制品: grailsApplication.getArtefactByLogicalPropertyName("Controller", "book") 但是我如何处理这个人工制品呢?您应该能够调用您检索到的人工制品上的new

我有一个过滤器和
controllerName
var获取控制器目标

例如: 当用户尝试访问
/myApp/book/index
时,我的过滤器被触发,
controllerName
等于
book
。如何获取BookController实例

Tks


编辑:

我可以使用以下方法获得
人工制品

grailsApplication.getArtefactByLogicalPropertyName("Controller", "book")

但是我如何处理这个人工制品呢?

您应该能够调用您检索到的人工制品上的
newInstance
newInstance
的工作原理与构造函数类似,因此您可以向普通构造函数调用提供任何参数

因此,您可以这样做:

def bookController = grailsApplication.getArtefactByLogicalPropertyName("Controller", "book").newInstance()

控制器将注册为Springbean。只需说出它的名字:

applicationContext.getBean('mypackage.BookController') // or
def artefact = grailsApplication.getArtefactByLogicalPropertyName("Controller", "book")
applicationContext.getBean(artefact.clazz.name)

正如Burt所说,您可能不希望在过滤器中有一个控制器实例。这是解决问题的错误方法

Grails控制器是由Spring框架自动注入的,在创建它时有一些黑魔法和过程。所以,我可以向你保证,这不是解决这个问题的方法

正如您自己所描述的,您希望调用您的操作,我可以想象您正试图重用驻留在您的操作中的一些代码,可能是为了在数据库中生成一些数据,甚至是为了使用HTTP会话,对吗

所以,你可以做两件事来解决这类问题

1) 只需将请求流重定向到控制器/操作,如下所示:

if (something) {
  redirect controller: 'xpto', action: 'desired'
  return false
}
2) 或者,您可以在您的操作(即执行您想要运行的脏作业)中获取逻辑,在一个服务中分离该逻辑,并通过以下方式在两个类(操作/服务)中重用该服务:

MyService.groovy

class MyService { 
  def methodToReuse() {
    (...)
  }
}
class MyController {

  def myService //auto-injected by the green elf

  def myAction = {
    myService.methodToReuse()
  }
}
class MyFilters {

  def myService //auto-injected by the red elf

  (...)
  myService.methodToReuse()
  (...)

}
MyController.groovy

class MyService { 
  def methodToReuse() {
    (...)
  }
}
class MyController {

  def myService //auto-injected by the green elf

  def myAction = {
    myService.methodToReuse()
  }
}
class MyFilters {

  def myService //auto-injected by the red elf

  (...)
  myService.methodToReuse()
  (...)

}
MyFilters.groovy

class MyService { 
  def methodToReuse() {
    (...)
  }
}
class MyController {

  def myService //auto-injected by the green elf

  def myAction = {
    myService.methodToReuse()
  }
}
class MyFilters {

  def myService //auto-injected by the red elf

  (...)
  myService.methodToReuse()
  (...)

}
[]s,

工作代码:

import org.codehaus.groovy.grails.web.context.ServletContextHolder
import org.codehaus.groovy.grails.web.servlet.GrailsApplicationAttributes
import org.springframework.context.ApplicationContext

ApplicationContext applicationContext = (ApplicationContext) ServletContextHolder.getServletContext().getAttribute(GrailsApplicationAttributes.APPLICATION_CONTEXT)
def grailsApplication

String nameController = "search"
def artefact = grailsApplication.getArtefactByLogicalPropertyName("Controller", nameController)
def controller = applicationContext.getBean(artefact.clazz.name)

为什么需要控制器实例?听起来像是一个设计缺陷。我需要从我的过滤器中调用一个操作,然后重定向到外部应用程序(准备我的应用程序以接收未来的请求)。换句话说,在重定向之前,我需要更新我的模型。@lucastex,tks供您解释。我在一个永恒的项目中,所以我需要最快的方法来实现这一点-你知道我在说什么:)。但在下一个版本中,我将遵循您的提示,使用服务来重用代码。有什么比将操作代码复制到新文件并在这两个方面使用此文件更快呢?如果您“实例化”一个新的控制器,您可能会遇到一些其他奇怪的问题,因为它都是由框架管理的。