Search code examples
grailsgrails-3.0grails-3.3

How to override redirect of grails 3 controller


I want to override(extend) the redirect method of a grails 3 controller.

In grails 2 this was done by overriding the method via metaClass. See Override Grails redirect method

Since grails 3 this is not working anymore.

What I want to achieve: I want to manipulate the argument map that is passed to the redirect method of every controller I implemented (filtered by package name)

Or to be more specific: I want to add/change the mapping param based on some small logic


Solution

  • I want to override(extend) the redirect method of a grails 3 controller.

    You can simply override the method as per usual language rules...

    class DemoController {
    
        // ...
    
        void redirect(Map m) {
            // do whatever you like here...
        }
    }
    

    If you want to invoke the original redirect method you can do that too but you will need to explicitly implement the Controller trait...

    import grails.artefact.Controller
    
    class DemoController implements Controller {
    
        void redirect(Map m) {
            // do whatever you like here before
            // invoking the original redirect...
    
            // invoke the original redirect...
            Controller.super.redirect m
        }
    }