views:

134

answers:

2

Hi,

In a Grails application I'm looking for some way to pass data from a controller action to a filter that runs after the action. I was thinking of something like:

class MyController {
    def myAction = {
        render(view:"myView", model:[key: "value"])

        passData {
            // Do some processing here
            name = "paolo"
            age = 38
        }
    }
}

public class MyFilters {

    def name
    def age

    def filters = {
        myFilter(controller: "*", action: "*") {
            after = { model ->
                // Do something with name and age
                return true
            }
        }
    }
}

The original plan was to do the following in the init() closure of BootStrap.config:

  • Use meta-programming to add a "passData(Closure pdClosure)" method to all the controllers
  • Set the delegate of pdClosure to MyFilters, so that when the name and age properties are set within this closure, they are set on the MyFilters instance.

However, I realised this won't work as there's no obvious way for me to access (from BootStrap.init) the MyFilters instance that will be called for a particular controller.

Equally, there doesn't appear to be any way to access the controller instance from within the filter. Of course, I could just stuff all the data into the model, but I'm wondering if there's a more elegant way to pass data between the two?

Thanks, Don

+3  A: 

Passing your data in the model seems pretty darn elegant and easy to me. You can even remove the data from the model in the filter if you don't want it to be available to the view for some reason.

Ben Williams
A: 

If in your filter you want to modify the data that is a model, then your solution looks fine.

If however you just want to pass some parameters from controller to the filter then request attributes should be better (if you need that data only the for request scope). This way you don't mix the model with some request parameters.

Mateusz Mrozewski