Search code examples
grailsservlet-filtersurl-mapping

Setting up the filter without controllers


I have a running tomcat at localost.

I wish to write a grails filter such that when ever user goes to localhost/filter/ intercept the call and do some processing. I created a filter which is inside conf folder

class TestFilters {

    def filters = {
        filter(uri:'/filter') {
            before = {
            }
            after = {                    
            }
            afterView = {                   
            }
        }
    }        
}

after setting this up when I go to localhost/filter/ I get only 404 error.

Where I'm making the mistake?

Thanks in advance


Solution

  • If you have no FilterController the url localhost/filter has no ressource to show - so you get a 404 error. You have to adapt your UrlMappings so that localhost/filter is a valid url of application.

    Add the following to UrlMappings.groovy:

    "/filter" (controller: "yourController", action:"yourAction") 
    

    Now - the url localhost/filter points to yourAction in yourController and the filter should be applied.