myborobudur
myborobudur

Reputation: 4435

To which controller play delegates to?

Can I find out, to which controller play delegates to?

In this scenario:

public class Menus extends Action.Simple {

    public Result call(Http.Context ctx) throws Throwable {
        ctx.args.put("menus", Menu.find.all());
        return delegate.call(ctx);
    }

    public static List<Menu> current() {
        return (List<Menu>)Http.Context.current().args.get("menus");
    }
}

can I find out in the call method which controller will be used?

Upvotes: 1

Views: 767

Answers (1)

estmatic
estmatic

Reputation: 3449

You could do it by writing a custom annotation for your action composition. See the section labeled Defining custom action annotations on the documentation page below.

http://www.playframework.com/documentation/2.1.1/JavaActionsComposition

Basically, on your annotation interface you would define a parameter to note the controller. Maybe just a simple String and you pass in the controller class name.

@With(MenusAction.class)
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Menus {
   String value();
}

When you annotate your controller or action methods you pass in the controller name value:

@Menus("MyController")
public static Result index() {
  return ok();
}

So now in your Action class you can just read the value and do whatever logic you're wanting to do.

public class MenusAction extends Action<Menus> {
  public Result call(Http.Context ctx) {
    if("MyController".equals(configuration.value)) {
      // do something
    }

    ctx.args.put("menus", Menu.find.all());
    return delegate.call(ctx);
  }
}

Upvotes: 1

Related Questions