这个概念的主要问题是注释的参数必须是一个常量,@see topic about this 所以你将不能像代码中显示的那样使用userId。相反,您可以创建一个读取上下文本身的注释,然后解析 URI 以获取用户的 ID。即:
app/myannotations/MyAnnotations.class
package myannotations;
import play.mvc.With;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
public class MyAnnotations {
@With(ValidateUserIdAction.class)
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ValidateUserId {
String patternToReplace();
String redirectTo();
}
}
app/myannotations/ValidateUserIdAction.class
package myannotations;
import play.mvc.Action;
import play.mvc.Http;
import play.mvc.Result;
import static play.libs.F.Promise;
public class ValidateUserIdAction extends Action<MyAnnotations.ValidateUserId> {
public Promise<Result> call(Http.Context ctx) throws Throwable {
boolean isValid = false;
// This gets the GET path from request
String path = ctx.request().path();
try {
// Here we try to 'extract' id value by simple String replacement (basing on the annotation's param)
Long userId = Long.valueOf(path.replace(configuration.patternToReplace(), ""));
// Here you can put your additional checks - i.e. to verify if user can be found in DB
if (userId > 0) isValid = true;
} catch (Exception e) {
// Handle the exceptions as you want i.e. log it to the logfile
}
// Here, if ID isValid we continue the request, or redirect to other URL otherwise (also based on annotation's param)
return isValid
? delegate.call(ctx)
: Promise.<Result>pure(redirect(configuration.redirectTo()));
}
}
因此您可以将其用于您的操作,例如:
@MyAnnotations.ValidateUserId(
patternToReplace = "/user/",
redirectTo = "/redirect/to/url/if/invalid"
)
public static Result getUser(userId){
....
}
当然,这是非常基本的示例,您可能希望/需要使您的 validationAction 类中的条件更加复杂,或者添加更多参数以使其更加通用,一切都取决于您。