How can I call a method in one of my controller classes without grails trying to generate a view?
-
where are you calling from and what are you trying to do – nate_weldon Mar 31 '12 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 '12 at 22:28
-
This question is perfectly valid. It's in the vein of what LinkedIn is doing with Scala and composing responses: http://engineering.linkedin.com/play/composable-and-streamable-play-apps – cdeszaq Jan 22 '14 at 22:21
3 Answers
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,825
- 2
- 29
- 35
class FooController {
def fooAction() {
render("Successful call to fooAction")
}
}

- 53,861
- 28
- 137
- 147

- 3,202
- 4
- 25
- 36
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...

- 1,714
- 2
- 17
- 21