Reputation: 21
How to call in grails framework one class method to another class method? example...
class ChartClassController {
def index() {}
def create(){
def chartClassTypeList = ChartClassType.list()
}
}
class ChartGroupController {
def index() {}
def create(){
def chartGroupTypeList = ChartGroupType.list()
}
}
I just call class (ChartGroupController) method (create) in ChartClassController ?? How is it possible ?? NEED A HELP !!
Upvotes: 1
Views: 735
Reputation: 8414
You could use a redirect (whether it makes sense from a user perspective is debatable):
def create(){
redirect(class:"ChartGroupController", action:"create")
}
Also, there is nothing to stop you from simply calling any domain class in any controller, so your ChartClassController
could have:
def create(){
def chartGroupTypeList = ChartGroupType.list()
}
and you could even use a view from your ChartGroup
directory if you wished. But you probably want to keep your "domains" separated for simplicity and readability.
Upvotes: 1
Reputation: 75681
Controllers aren't regular classes, they're basically event handlers responding to requests. The life cycle is managed by Grails and Spring, and they're basically not directly callable.
Rather than calling one controller from another, consider moving the logic from that method that you want in both controllers to a utility class under src/groovy. Then call it from the two controllers.
Another option would be to create a base class that both controllers extend, and put that shared logic in a base class method that both controllers call from their create methods.
Upvotes: 4