Can't I just call then function "second" with no redirect action if I in the same class to get the same result i would get with redirect action in the following code?
class SampleController {
def first() {
// redirect to the "second" action...
redirect action: "second"
}
def second() {
// ...
} }
Can't I just call then function "second" with no redirect action if I in the same class to get the same result i would get with redirect action in the following code?
Of course you can call a method from within a controller action but whether or not you get the same result depends on a number of factors. The best way to approach this is to understand the differences between a redirect, a forward and invoking a method. Understanding those will help you understand when each of them makes sense.
When you initiate a redirect the response is sent back to the client with information which causes the client to send a separate request back to your application. A full HTTP round trip is made. A forward is similar to a redirect except it all happens within the first request. Control is sent to the forwarded action without needing to go back to the client and having it send a second request. Invoking a method is different than all of that. Invoking a method is just invoking a method and there are a number of things that won't happen when you do that. For example, filters (Grails filters or regular servlet filters) will not be involved in that method call, even if the method you are calling is a controller action and there are filters configured that would normally apply to calls to that action. You really should never invoke a controller action method directly. If the method includes helper code that you might want to call, put that code in a regular non-action method (make it non-public) and then invoke it from wherever that makes sense. Usually a better idea is to move that method out of the controller altogether and put it in a helper class where it will have a number of a benefits including that it can be re-used wherever appropriate, tested separate from a controller, etc. This helper class might or might not be a Grails service, depending on how it is used and what it is doing.
I hope that helps.