Help us understand the problem. What is going on with this article?

Play2.5 (Java)でcontrollerの処理の後に処理を実行するハンドラの書き方

More than 3 years have passed since last update.

アクションを合成するカスタムアノテーションを作成し、そこに、アクションが完了した後に実行される処理を記述します。

つまり、「アクションが終わった後に処理が実行されるアクション」を作成し、任意のコントローラーのメソッドに合成します。

※正しい(推奨されている)書き方なのかどうか自信はありませんが……

ソース

HogeController.java
package controllers;

import javax.inject.Singleton;

import play.mvc.Controller;
import play.mvc.Result;
import play.mvc.With;

@Singleton
public class HogeController extends Controller {

    @With(HogeAction.class)
    public Result index() {
        System.out.println("--------------------------");
        System.out.println("HogeController.index");
        System.out.println("--------------------------");
        return ok();
    }
}
HogeAction.java
package controllers;

import java.util.concurrent.CompletionStage;

import play.mvc.Action;
import play.mvc.Http.Context;
import play.mvc.Result;

public class HogeAction extends Action.Simple {

    @Override
    public CompletionStage<Result> call(Context ctx) {

        System.out.println("before action");

        return delegate.call(ctx)
                .whenComplete((result, t) -> {

                    System.out.println("status:" + result.status());
                    System.out.println("after action");

                });
    }

}

実行結果

before action
--------------------------
HogeController.index
--------------------------
status:200
after action

参考URL

kunst1080
Play + Javaで小規模WEBサービス作ってるかも
http://kunst1080.hatenablog.com/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした