Simple trick to DRY your Grails controller

Grails controllers are not very DRY. It’s easy to find duplicated code fragments in default generated controller. Take a look at code sample below. It is duplicated four times in show, edit, update and delete actions: class BookController {def show() {def bookInstance = Book.get(params.id)if (!bookInstance) {flash.message = message(code: 'default.not.found.message', args: [message(code: 'book.label', default: 'Book'), params.id])redirect(action: "list")return } [bookInstance: bookInstance] }} Why is it duplicated? There is a reason for that duplication, though. If you move this snippet to a method, it can redirect to "list" action, but it can’t prevent controller from further execution. After you call redirect, response status changes to 302, but after method exits, controller still runs subsequent code. Solution At TouK we’ve implemented a simple trick to resolve that situation: wrap everything with a simple withStoppingOnRender method, whenever you want to render or redirect AND stop controller execution – throw EndRenderingException. We call it Big Return – return from a method and return from a controller at once. Here is how it works: class BookController {def show(Long id) {withStoppingOnRender {Book bookInstance = Book.get(id)validateInstanceExists(bookInstance) [bookInstance: bookInstance] } }protected Object withStoppingOnRender(Closure closure) {try {return closure.call() } catch (EndRenderingException e) {} }private void validateInstanceExists(Book instance) {if (!instance) {flash.message = message(code: 'default.not.found.message', args: [message(code: 'book.label', default: 'Book'), params.id])redirect(action: "list")throw new EndRenderingException() } }}class EndRenderingException extends RuntimeException {} Example usage For simple CRUD controllers, you can use this solution and create some BaseController class for your controllers. We use withStoppingOnRender in every controller so code doesn’t look like a spaghetti, we follow DRY principle and code is self-documented. Win-win-win! Here is a more complex example: class DealerController {@Transactionaldef update() {withStoppingOnRender {Dealer dealerInstance = Dealer.get(params.id)validateInstanceExists(dealerInstance)validateAccountInExternalService(dealerInstance)checkIfInstanceWasConcurrentlyModified(dealerInstance, params.version) dealerInstance.properties = paramssaveUpdatedInstance(dealerInstance)redirectToAfterUpdate(dealerInstance) } }}

Grails controllers are not very DRY. It’s easy to find duplicated code fragments in default generated controller. Take a look at code sample below. It is duplicated four times in show, edit, update and delete actions:

class BookController {
    def show() {
       def bookInstance = Book.get(params.id)
       if (!bookInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'book.label', default: 'Book'), params.id])
            redirect(action: "list")
            return
        }
        [bookInstance: bookInstance]
    }
}

Why is it duplicated?

There is a reason for that duplication, though. If you move this snippet to a method, it can redirect to "list" action, but it can’t prevent controller from further execution. After you call redirect, response status changes to 302, but after method exits, controller still runs subsequent code.

Solution

At TouK we’ve implemented a simple trick to resolve that situation:

  1. wrap everything with a simple withStoppingOnRender method,
  2. whenever you want to render or redirect AND stop controller execution – throw EndRenderingException.

We call it Big Return – return from a method and return from a controller at once. Here is how it works:

class BookController {
    def show(Long id) {
        withStoppingOnRender {
            Book bookInstance = Book.get(id)
            validateInstanceExists(bookInstance)[bookInstance: bookInstance]
        }
    }
    private void validateInstanceExists(Book instance) {
        if (!instance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'book.label',
                default: 'Book'), params.id])
            redirect(action: "list")
            throw new EndRenderingException()
        }
    }
}
class EndRenderingException extends RuntimeException {}

Example usage

For simple CRUD controllers, you can use this solution and create some BaseController class for your controllers. We use withStoppingOnRender in every controller so code doesn’t look like a spaghetti, we follow DRY principle and code is self-documented. Win-win-win! Here is a more complex example:

class DealerController {
    @Transactional
    def update() {
        withStoppingOnRender {
            Dealer dealerInstance = Dealer.get(params.id)
            validateInstanceExists(dealerInstance)
            validateAccountInExternalService(dealerInstance)
            checkIfInstanceWasConcurrentlyModified(dealerInstance, params.version)
            dealerInstance.properties = params
            saveUpdatedInstance(dealerInstance)
            redirectToAfterUpdate(dealerInstance)
        }
    }
}
You May Also Like

Grails render as JSON catch

One of a reasons your controller doesn't render a proper response in JSON format might be wrong package name that you use. It is easy to overlook. Import are on top of a file, you look at your code and everything seems to be fine. Except response is still not in JSON format.

Consider this simple controller:

class RestJsonCatchController {
def grailsJson() {
render([first: 'foo', second: 5] as grails.converters.JSON)
}

def netSfJson() {
render([first: 'foo', second: 5] as net.sf.json.JSON)
}
}

And now, with finger crossed... We have a winner!

$ curl localhost:8080/example/restJsonCatch/grailsJson
{"first":"foo","second":5}
$ curl localhost:8080/example/restJsonCatch/netSfJson
{first=foo, second=5}

As you can see only grails.converters.JSON converts your response to JSON format. There is no such converter for net.sf.json.JSON, so Grails has no converter to apply and it renders Map normally.

Conclusion: always carefully look at your imports if you're working with JSON in Grails!

Edit: Burt suggested that this is a bug. I've submitted JIRA issue here: GRAILS-9622 render as class that is not a codec should throw exception