2
votes

I don't understand the meaning of @With annotation in Play Java. We have the same question in StackOverflow that seems to be Play1, not Play2. And in Play's document of the latest version, I saw this example.

@With(VerboseAction.class)
public Result verboseIndex() {
    return ok("It works!"); }

What does it mean? In the above case, what is the difference between with the annotation and without the annotation?

1

1 Answers

2
votes

It's obvious that @With is using for composing Actions:

public class VerboseAction extends play.mvc.Action.Simple {
    public CompletionStage<Result> call(Http.Context ctx) {
        Logger.info("Calling action for {}", ctx);
        return delegate.call(ctx);
    }
}

You can compose the code provided by the action method with another play.mvc.Action, using the @With annotation:

@With(VerboseAction.class)
public Result verboseIndex() {
    return ok("It works!");
}

So, when verboseIndex will be called, at first method call of VerboseAction will be called. So, in this example, first the Logger.info will write some info message, and then later ok response will be completed.