简体   繁体   English

在动作组合中获取url参数

[英]get url params in Action compositions

I have a Interceptor (Action compositions) defined for my controllers. 我为控制器定义了一个拦截器(动作组合)。 I need to access my url params in the request 我需要在请求中访问我的网址参数

ie for an entry in conf below, how would I access the Id paramter inside my Action composition? 例如,对于下面的conf中的条目,我将如何访问Action组成内的Id参数?

GET /jobs/:id controllers.JobManager.getlist(id: Int)

my Action method interceptor class only has reference to my Http.Context object. 我的Action方法拦截器类仅引用了我的Http.Context对象。 while access to the body of the request is evident the url param is not. 虽然访问请求的主体很明显,但url参数却没有。

Extract it yourself. 自己提取它。 In your example path is 6 characters long plus length of id. 在您的示例路径中,长度为6个字符,加上ID的长度。

String path = ctx.request().path();
String id = path.substring(6, path.length());

This solution depends on route's length. 该解决方案取决于路线的长度。 Alternatively, you can pass the start and end parameters to your action: 另外,您可以将start和end参数传递给您的操作:

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

    int start() default -1;
    int end() default -1;

}

And use it in action class to extract parameter: 并在动作类中使用它来提取参数:

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);
    }
}

Usage in JobManager controller: 在JobManager控制器中的用法:

@Args(start = 6)
public getlist(Integer id) {
    return ok();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM