在 Play 框架中使用 @With 注解组合动作 (Java)
Action composition using @With annotation in Play framework (Java)
如何在 Play Framework 2.4(在 Java 中)中使用两个动作组合?
假设,为了避免代码重复,我有两个操作要使用:Auth
和 LogData
。
如何在动作组合中同时使用两者?
这不会编译,导致重复注释错误:
# play.PlayExceptions$CompilationException: Compilation error[error:
duplicate annotation]
@play.db.jpa.Transactional()
@With(Auth.class)
@With(LogData.class)
public static Result callForumTeacher(String random, Long gameId){
//Action code
return ok(Json.toJson("data"));
}
这是关于如何实现 Auth
和 LogData
的框架:
public class CheckPausedGame extends Action.Simple {
@Override
public F.Promise<Result> call(Http.Context context) throws Throwable {
if (checkCondition(context)) {
return delegate.call(context);
} else {
F.Promise<Result> promise = F.Promise.promise(new F.Function0<Result>() {
@Override
public Result apply() throws Throwable {
return redirect("/paused");
}
});
return promise;
}
}
}
这只是一个框架,省略了一些对这个问题没有用的方法。
虽然文档似乎没有明确说明这一点(至少我没有在任何地方找到它),但在这种情况下使用 @With
的预期方式是一次传递所有操作( With
接受一个数组)
您的代码变为
@play.db.jpa.Transactional()
@With(value = {Auth.class, LogData.class})
public static Result callForumTeacher(String random, Long gameId){
//Action code
return ok(Json.toJson("data"));
}
如何在 Play Framework 2.4(在 Java 中)中使用两个动作组合?
假设,为了避免代码重复,我有两个操作要使用:Auth
和 LogData
。
如何在动作组合中同时使用两者?
这不会编译,导致重复注释错误:
# play.PlayExceptions$CompilationException: Compilation error[error: duplicate annotation]
@play.db.jpa.Transactional()
@With(Auth.class)
@With(LogData.class)
public static Result callForumTeacher(String random, Long gameId){
//Action code
return ok(Json.toJson("data"));
}
这是关于如何实现 Auth
和 LogData
的框架:
public class CheckPausedGame extends Action.Simple {
@Override
public F.Promise<Result> call(Http.Context context) throws Throwable {
if (checkCondition(context)) {
return delegate.call(context);
} else {
F.Promise<Result> promise = F.Promise.promise(new F.Function0<Result>() {
@Override
public Result apply() throws Throwable {
return redirect("/paused");
}
});
return promise;
}
}
}
这只是一个框架,省略了一些对这个问题没有用的方法。
虽然文档似乎没有明确说明这一点(至少我没有在任何地方找到它),但在这种情况下使用 @With
的预期方式是一次传递所有操作( With
接受一个数组)
您的代码变为
@play.db.jpa.Transactional()
@With(value = {Auth.class, LogData.class})
public static Result callForumTeacher(String random, Long gameId){
//Action code
return ok(Json.toJson("data"));
}