What is the best way to transfer data from filter to controller?

Hi, in part of our application, I need to pass data from a filter to a controller. I read that it can be done using the request object and before the interceptor on the controller.

Sample code:

class SomeService {
    def doSomething(request, params) {
        request.foo = "helloworld"
    }
}

class SomeFilter {

    def someService

    def filters = {
        all(controller:'*', action:'*') {
            before = {
                // service does something and places object in request
                // using request.foo = "helloworld"
                someService.doSomething(request, params)
            }
        }
    }
}

class SomeController {

    def foo

    def beforeInterceptor = {
        foo = request.foo
    }

    def index = { 
        println foo
    }
}

      

Is this an efficient way of doing things or are there other ways?

+3


source to share


1 answer


The method you are using (assigning data to a request attribute) is an efficient and widely used way of passing request data between tiers.



The grail-based Spring framework takes advantage of this extensively. You can see some of the attribute keys used here: http://static.springsource.org/spring/docs/3.0.x/api/org/springframework/web/util/WebUtils.html

+3


source







All Articles