6

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

3
  • where are you calling from and what are you trying to do Mar 31, 2012 at 20:42
  • An action in a controller always has to send something back to the user. But it doesn't have to be a GSP page, or HTML at all, you can construct a response of any type yourself. As nate suggests, we need to know what you want to achieve to be able to help you find a solution.
    – David
    Mar 31, 2012 at 22:28
  • This question is perfectly valid. It's in the vein of what LinkedIn is doing with Scala and composing responses: engineering.linkedin.com/play/…
    – cdeszaq
    Jan 22, 2014 at 22:21

3 Answers 3

4

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.

3
class FooController {

  def fooAction() {
    render("Successful call to fooAction")
  }

}
0

Essentially you can create a controller instance (via 'new' keyword) and then call the action of interest. Please provide more details about what you want to do, so i might be able to give a better answer...

Not the answer you're looking for? Browse other questions tagged or ask your own question.