【问题标题】:Grails filters not being called for custom actions没有为自定义操作调用 Grails 过滤器
【发布时间】:2015-05-18 14:30:53
【问题描述】:

我正在尝试实现一个过滤器,在进入 GSP 页面之前应该在某些情况下调用该过滤器。由于 URL 在我希望它发生的实例和我不希望它发生的实例之间没有太大差异,我认为最好的方法是创建一个什么都不做的方法(里面有一个 print 语句) - 但可以在我希望过滤操作发生时简单地调用。

我都试过了:

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',我会看到过滤器中的打印被调用。

【问题讨论】:

    标签: grails filter grails-filters


    【解决方案1】:

    您面临的问题是您直接从控制器中的另一个操作调用hello() 操作。这只是一个普通的方法调用,不会通过过滤器。

    Grails 过滤器在 HTTP 客户端请求特定 URI 时被调用,例如http://localhost:8080/my-app/myController/myAction 将匹配 myControllermyAction

    如果您在响应不同的 URI 时只是从控制器内调用 myAction(),则不会使用过滤器。你在做什么。

    【讨论】:

    • 谢谢 - 我没有意识到必须进行 HTTP 调用。似乎使用 beforeInterceptor 可以解决问题?
    • beforeInterceptor 也将基于 HTTP 请求调用的操作。就像一个过滤器。
    【解决方案2】:

    你是这么说的:

    只需添加即可调用这些

    你好()

    这是否意味着您正在从另一个方法调用 hello() 方法?确保为了测试过滤器,您需要直接点击 URL /userProfile/home

    【讨论】:

    • 是的,我的意思是我正在从另一个调用 hello() 方法。我不确定我是否理解您的第二点——家从何而来?
    • 从另一个方法调用hello() 不会调用过滤器。这只是一个方法调用。过滤器通过网络请求调用。
    猜你喜欢
    • 2012-01-10
    • 2017-06-14
    • 2022-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-05
    • 2023-04-01
    • 1970-01-01
    相关资源
    最近更新 更多