简体   繁体   中英

Grails filters not being called for custom actions

I'm trying to implement a filter that should be called in certain instances before going to a GSP page. As the URL does not differ a lot between the instances where I want it to happen, and those where I don't, I thought the best way to do this would be to create a method that does nothing (with a print statement inside) - but can simply be called when I want the filter action to take place.

I have tried this as both:

def hello(){
    print "hello"       
}

and

def hello = {
    print "hello"
}

calling these simply by adding

hello()

at the relevant point

The start of my filter is as follows:

import uui.FormattingService

class TimeFormatterFilters {

    def FormattingService formattingService

    def filters = {
        someFilter(controller: 'userProfile', action: 'hello') {
            before = {
                print "filter action taking place"

I do not see the print statement from within the filter for either of the newly made methods within the UserProfileController, yet if I swap the action for the filter to 'index' , I see the print within the filter being called.

you said this :

calling these simply by adding

hello()

Does this mean that you are calling the hello() method from another method ? Make sure that in order to test filters you need to hit the URL directly /userProfile/home

The problem you are facing is that you are calling the hello() action directly from another action within your controller. This is just a normal method call and does not pass through the filter.

Grails filters are called when an HTTP client requests a specific URI eg http://localhost:8080/my-app/myController/myAction will match myController and myAction .

If you simply call myAction() from within the controller when responding to a different URI the filter won't be used. Which is what you are doing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM