Java 获取动作组合中的url参数
我为我的控制器定义了一个拦截器(动作合成)。 我需要访问请求中的url参数 i、 e.对于下面conf中的条目,我如何访问动作组合中的Id参数Java 获取动作组合中的url参数,java,playframework,playframework-2.0,Java,Playframework,Playframework 2.0,我为我的控制器定义了一个拦截器(动作合成)。 我需要访问请求中的url参数 i、 e.对于下面conf中的条目,我如何访问动作组合中的Id参数 GET /jobs/:id controllers.JobManager.getlist(id: Int) 我的操作方法拦截器类只引用了我的Http.Context对象。虽然对请求主体的访问是显而易见的,但url参数不是。自己提取它。在您的示例中,路径是6个字符加上id的长度 String path = ctx.request().path(); St
GET /jobs/:id controllers.JobManager.getlist(id: Int)
我的操作方法拦截器类只引用了我的Http.Context对象。虽然对请求主体的访问是显而易见的,但url参数不是。自己提取它。在您的示例中,路径是6个字符加上id的长度
String path = ctx.request().path();
String id = path.substring(6, path.length());
此解决方案取决于路线的长度。或者,您可以将开始和结束参数传递给操作:
@With({ ArgsAction.class })
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
public @interface Args {
int start() default -1;
int end() default -1;
}
并在action类中使用它来提取参数:
public class ArgsAction extends Action<Args> {
@Override
public Promise<Result> call(Context ctx) throws Throwable {
final int start = configuration.start();
final int end = configuration.end();
if (start != -1) {
final String path = ctx.request().path();
String arg = null;
if (end != -1) {
arg = path.substring(start, end);
} else {
arg = path.substring(start, path.length());
}
// Do something with arg...
}
return delegate.call(ctx);
}
}
@Args(start = 6)
public getlist(Integer id) {
return ok();
}