grailsfiltergrails-filters

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.


Solution

  • 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 e.g. 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.