我正在尝试实现一个过滤器,该过滤器在转到GSP页面之前应在某些情况下调用。由于URL在我希望发生的实例与我不希望发生的实例之间没有太大差异,因此我认为做到这一点的最佳方法是创建一个不执行任何操作的方法(内部带有打印语句) -但当我希望执行过滤操作时可以简单地调用它。
我已经尝试过这两个:
def hello(){
print "hello"
}
和
def hello = {
print "hello"
}
通过添加简单地调用这些
hello()
在相关点
我的过滤器的开始如下:
import uui.FormattingService
class TimeFormatterFilters {
def FormattingService formattingService
def filters = {
someFilter(controller: 'userProfile', action: 'hello') {
before = {
print "filter action taking place"
我没有看到UserProfileController中两个新创建的方法从过滤器中看到的print语句,但是如果我将过滤器的操作交换为
'index'
,则会看到过滤器中的打印被调用。
最佳答案
您面临的问题是您正在直接从 Controller 中的另一个 Action 调用hello()
Action 。这只是一个普通的方法调用,不会通过过滤器。
当HTTP客户端请求特定URI(例如, http://localhost:8080/my-app/myController/myAction
将匹配myController
和myAction
。
如果您仅在响应其他URI时从 Controller 内部调用myAction()
,则不会使用该过滤器。你在做什么
关于grails - 没有为自定义操作调用Grails筛选器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30305956/