Search code examples
grailscontrollers

Grails controller syntax correction


I have something like this in my controller:

class houseController = {

...
code
...

if(params.answer == null)
redirect(action:'xxx')

...
code
...

}

My doubt is, the redirect in the middle of the controller. Does it need any 'return' or something similar due to not being at the end of the controller ? Or, after the redirect is done, all code after that is forgoten and not kept in memory ? My point, i dont wanna waste useless resources with bad-written code.


Solution

  • Any code that occurs after the redirect will be executed, but you will get an exception if you try to write to the response after performing the redirect.

    In practice, you don't usually want to execute anything in the current action after performing the redirect, so I would rewrite the code above as:

    if (params.answer == null)
      redirect(action:'xxx')
      return
    }
    
    ...
    code
    ---
    

    or alternatively:

    if (params.answer == null)
      redirect(action:'xxx')
    
    } else {
      ...
      code
      ---
    }