Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 332633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:53:05+00:00 2026-05-12T09:53:05+00:00

Grails provides filters that run before your controllers. They’re defined in classes that look

  • 0

Grails provides filters that run before your controllers. They’re defined in classes that look like this:

class SecurityFilters {
   def filters = {
       myFilter(controller:'*', action:'*') { // What are those weird colons??
           print "I'm filtering!"
           // Code that does the filtering goes here
       }
   }
}

These work great but I would like to understand the syntax better as it doesn’t look like any Groovy code I’ve seen before. In particular, the line above that starts with myFilter seems very odd. Is this a method definition for a method called myFilter? If so, what does :'*' mean after each parameter? I thought it might be a default parameter value but that would be ='*'. I’ve seen named parameters using colons in method calls before but this couldn’t be a method call because I haven’t defined myFilter() anywhere else.

I think I’d understand much better if someone could just tell me how to execute the filtering code from a normal Groovy class. In other words, if I have a file MyFilters.groovy that contains the lines above, how could I finish this Groovy code so it prints “I’m filtering”?

import MyFilters
def mf = new MyFilters()
mf.filters.somethingGoesHere // Help! How do I finish this line so it calls my filtering code?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T09:53:06+00:00Added an answer on May 12, 2026 at 9:53 am

    The following Groovy code would print “I’m filtering!”:

    class SecurityFilters {
       def filters = {
           myFilter(controller:'*', action:'*') { // What are those weird colons??
               print "I'm filtering!"
               // Code that does the filtering goes here
           }
       }   
    }
    
    class FilterDelegate {
        def methodMissing(String methodName, args) {
            // methodName == myFilter
            // args[0] == [controller:*, action:*]
            // args[1] == {print "I'm filtering!"}
            args[1].call()
        }
    }
    
    def sf = new SecurityFilters()
    def filtersClosure = sf.filters
    filtersClosure.delegate = new FilterDelegate()
    filtersClosure.call()
    

    In this example filters is a closure that calls a method named myFilter and passes a map and a closure. You can think of myFilter as:

    myFilter([controller:'*', action:'*'], closure)
    

    The Map can contains keys like controller, action or uri. The wildcard (*) is used when Grails attempts to match the URI from the HTTP request when it tries to determine which closure to call.

    My understanding of how Grails handles filters is that a delegate loader class is used. The loader class provides a methodMissing method and creates a FilterConfig for each method call inside the filters closure. When a HTTP request is made, Grails looks through all the FilterConfig objects and tries to find a matching scope (looks in the map for a controller, action or uri and uses regexs to match on wildcards). If it finds a match it calls the closure that was passed into the method in the Filter class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I did a grails clean and afterwards when I run via grails run-app the
I'm new to grails. I'd like to give it a whirl by implementing a
Grails vs Rails. Which has better support? And which one is a better choice
Grails makes it very easy to configure datasources for different environments (development, test, production)
I'm still learning Grails and seem to have hit a stumbling block. Here are
I'm using Grails 1.1 beta2. I need to import a large amount of data
In a Grails domain class, how do I set the constraint for a String
I'm learning Grails/GORM and as I've understood it the current best practice is not
I have not seen much support for Grails to develop facebook apps.I was just
I'm currently working with Groovy and Grails. While Groovy is pretty straight-forward since it's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.