Jeremy -
Jeremy -

Reputation: 51

Play 2.2 java - can one controller call another?

Is it possible to call a Play controller from another controller's Action? I'm implementing a Controller in java like so:

public class SubdomainHandler extends Controller {
    public static Result redirect(String path, String file) {
        String newPath = ... // do tricky things with Http.Context.current().request().host() .
        Action wrappedControllerAction = Assets.at(newPath, file);

        return wrappedControllerAction.render(); // <- Nope, no such API.
    }
}

Is it possible to get an Action to return a Result? I'm assuming it's ok to do a synchronous call here, but returning a Promise seems the proper thing to do.

Cheers, j-

Upvotes: 2

Views: 1528

Answers (2)

Jeremy -
Jeremy -

Reputation: 51

In scala, the solution was trivial:

object SubdomainHandler extends Controller {

  def redirect(path: String, file: String) = controllers.Assets.at(messWithPath(path), messWithFile(file))

}

That took care of things for my use case in Play.

Anyone care to take a stab at the java equivalent, for the record?

Upvotes: 0

Seb Cesbron
Seb Cesbron

Reputation: 3833

There is no magic in play2 as there was in play1, controller methods are just methods so you can call them between controllers, you just have to render properly at the end.

I don't know what you want to do but if you want to have a common behaviour between multiple actions, ActionBuilder or Filters might be better for your use case

Upvotes: 1

Related Questions