Search code examples
grailsviewcontrollergsp

How to call an action without generating a view in grails


How can I call a method in one of my controller classes without grails trying to generate a view?


Solution

  • You can redirect to another controller action.

    class PuppyController {
    
       def woof() {
         redirect(action:'bark')
       }
    
       def bark(){
         response.write "Moo"
       }
    
    }
    

    At some point you should either write to the response or redirect to a method/closure that corresponds to a view so the user can receive the output.

    If the method you're trying to call is on another controller, chances are YOAR DOING IT WRONG.

    If, for example, I have a controller method that uploads a file, and another method that creates the filename for that file as a combination of some convention I make up (say timestamp + "pretty file for" + username) on another controller, you should promote that controller method to a Service and inject it into both controllers.