2012-10-02 20 views
9

私はPlay 1.2を使用していたとき、コントローラ内の各リクエストの前後にメソッドを実行するために、@Beforeまたは@After(および他のもの)のコントローラ内のメソッドに注釈を付けることができました。Play 1.2の@Beforeと@Afterに相当するPlay 2.0に相当するものは何ですか?

Play 2.0でどうすればいいですか?

私はグローバルオブジェクトについて少し読んでいますが、私が探しているものではないようです。また、行動の構成は、私がやりたいことに対してあまりにも複雑すぎるようです。私はもっ​​と簡単なものを見たいと思う。

アイデア?

答えて

8

残念ながら、@Beforeにはaction compositionを使用し、@Afterには同等のものはありません。

@Afterの場合、私は終了アクションの最後に私自身のafterメソッドを書きます。このような何か:

public static Result index() { 
    .... 
    Result result = ...; 
    return after(result); 
} 

protected static Result after(Result result) { 
    ... 
    Result afterResult = ..., 
    return afterResult 

} 
3
public class Logging { 

    @With(LogAction.class) 
    @Target({ElementType.TYPE, ElementType.METHOD}) 
    @Retention(RetentionPolicy.RUNTIME) 
    public @interface Logs { 

    } 

    public static class LogAction extends Action<Logs> { 

     private void before(Context ctx) { 
      System.out.println("Before action invoked"); 
     } 

     private void after(Context ctx) { 
      System.out.println("After action invoked"); 
     } 

     public F.Promise<Result> call(Http.Context context) throws Throwable { 
      before(context); 
      Promise<Result> result = delegate.call(context); 
      after(context); 
      return result; 
     } 
    } 

} 

あなたのコントローラで@Logsと注釈。

関連する問題