8.1 Controllers

Controllers are the entry point for your application's logic. Each controller has access to their model and view instances from their respective MVC group.

Controller actions are usually defined using a closure property form, like the following one

    class MyController {
        def someAction = { evt = null ->
            // do some stuff
        }
    }

It is also possible to define actions as methods, however the closure property form is preferred (but not enforced). The caveat is that you would need to translate the method into a MethodClosure when referencing them form a View script. In the following example the action 'action1' is defined as a closure property, whereas the action 'action2' is defined as a method

    application(title: 'Action sample', pack: true) {
        gridLayout(cols: 2, rows: 1) {
            button 'Action 1', actionPerformed: controller.action1
            button 'Action 2', actionPerformed: controller.&action2
        }
    }

Actions must follow these rules in order to be considered as such:

- must have public (Java) or default (Groovy) visibility modifier.
- name does not match an event handler, i.e, it does not begin with `on`.
- must pass `GriffonClassUtils.isPlainMethod()` if it's a method.
- must have `void` as return type if it's a method.
- value must be a closure (including curried method pointers) if it's a property.

Controllers can perform other tasks:

- listen to application events.
- create and destroy MVC groups via a pair of methods (createMVCGroup, destroyMVCGroup).
- react to MVC initialization/destruction via a pair of methods (mvcGroupInit, mvcGroupDestroy).
- hold service references.