LoginSignup
2
3

More than 5 years have passed since last update.

現在のコントローラ、アクション、パラメータ

Posted at

URL 同様、現在実行しているコントローラ、アクションも play.mvc.Http.Request から取得することができます。

String controller = Request.current().controllerClass.getName();
String action = Request.current().invokedMethod.getName();

また、アクションに引き渡されたパラメータは play.mvc.Scope.Params から取得することができます。

Map<String, String> params = Params.current().allSimple();

これを組み合わせて @Before でログを出力してみました。


@Before(priority = 0)
public static void before() {

    Request request = Request.current();

    Class controller = request.controllerClass;
    Method action = request.invokedMethod;

    List<String> types = new ArrayList<String>();
    for (Class c : action.getParameterTypes()) {
        types.add(c.getName());
    }

    List<String> values = new ArrayList<String>();
    Map<String, String> params = Params.current().allSimple();
    for (String key : params.keySet()) {
        if ("body".equals(key)) {
            continue;
        }
        values.add(String.format("%s=%s", key, params.get(key)));
    }

    String c = controller.getName();
    String a = action.getName();
    String t = StringUtils.join(types, ", ");
    String v = StringUtils.join(values, ", ");

    Logger.debug("BEFORE: %s.%s(%s)[%s]", c, a, t, v);
}
2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3