gpt4 book ai didi

java - 在 Action 组合中获取 url 参数

转载 作者:搜寻专家 更新时间:2023-11-01 03:37:19 24 4
gpt4 key购买 nike

我有一个为我的 Controller 定义的拦截器( Action 组合)。我需要在请求中访问我的 url 参数

即对于下面的 conf 中的条目,我将如何访问我的 Action 组合中的 Id 参数?

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

我的 Action 方法拦截器类只引用了我的 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);
}
}

在 JobManager Controller 中的用法:

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

关于java - 在 Action 组合中获取 url 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27152824/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com