Search code examples
grailsgroovygrails-controller

how to define a command object inside a controller


I have started to look into command object as a means for validating a form that has no domain associated with it. On my research, i found out that a command object either can be declared in the same package as my controller or in the controller class itself.

However, all the examples that I have seen so far have a separate command object class bot not inside the controller.

Can anyone show me an example or code to define a command object inside my controller itself?


Solution

  • There's nothing special about command object classes. You can declare them as an inner class inside your controller like this:

    class MyController {
    
        class ActionCommand {
            String paramName
        }
    
        def action(ActionCommand actionCommand) {
            render actionCommand.paramName
        }
    }