简体   繁体   English

在Play Java中编写动作

[英]Composing Action in Play Java

I know we have same types of questions in Stackoverflow, but to my best knowledge these are all written in Scala. 我知道在Stackoverflow中我们有相同类型的问题,但是据我所知,这些都是用Scala编写的。 In the official documentation , you can see how to compose Action. 官方文档中 ,您可以了解如何编写 Action。 Actually I am not clear of it, but 其实我不清楚,但是

public class VerboseAction extends play.mvc.Action.Simple {
public CompletionStage<Result> call(Http.Context ctx) {
    Logger.info("Calling action for {}", ctx);
    return delegate.call(ctx);
}

This above code is same with a below one? 上面的代码与下面的代码相同吗?

@With(VerboseAction.class)

public Result verboseIndex() {
    return ok("It works!"); }

And they write we have to delegate to the wrapped Action, but in above code which action is called? 他们写道,我们必须委托给包装好的Action,但是在上面的代码中,哪个Action被调用了? What does delegate.call(ctx) mean? delegate.call(ctx)是什么意思? I know Play's Action is an instance of Action , but the return value is actually CompletionStage<Result> , not Action . 我知道玩的是行动的一个实例Action ,但返回值实际上是CompletionStage<Result> ,没有Action
So, I am confused. 所以,我很困惑。 Maybe I have tiny misunderstandings, so could anyone point out it? 也许我有一点误会,所以有人可以指出吗?

Thanks. 谢谢。

An Action is a unit of work that provides a Result (in the end). Action是提供Result的工作单元(最后)。 Your first block of code constructs an Action that will execute the log statement, and then return with a call to the delegate , which will be the next Action unit (either another annotation or the controller method) to execute and calls that, passing the context along the entire time. 您的第一个代码块构造一个Action ,它将执行log语句,然后返回对delegate的调用,该delegate将是要执行的下一个Action单元(另一个注释或控制器方法),并通过上下文传递该delegate一直以来

Your misunderstanding in the last part of the question is that the return type of the call is not the next Action to undertake. 你的问题的最后一部分的误解是调用的返回类型是不是下一个Action来承担。 It is the (eventual) Result of all the Action s. 这是所有Action的(最终) Result

I will try to answer question by question: 我将尝试逐个问题地回答:

This above code is same with a below one? 上面的代码与下面的代码相同吗?

No, its actually composing both actions with "with" annotation. 不,它实际上是用“ with”注释组成这两个动作。

but in above code which action is called? 但是在上面的代码中,哪个动作被调用?

Action is process of serving response to request received. 动作是处理对收到的请求的响应的过程。 This action will be called whenever a request to this method receive (request are mapped to methods in routes file). 每当收到对此方法的请求时(此请求将映射到路由文件中的方法),将调用此操作。

And they write we have to delegate to the wrapped Action, What does delegate.call(ctx) mean? 他们写道,我们必须委托给包装好的Action,委托.call(ctx)是什么意思?

delegate is: 代表是:

/**
 * The wrapped action.
 */
"public Action<?> delegate;" 

and call is: 呼叫是:

/**
 * Executes this action with the given HTTP context and returns the result.
 */
"public abstract Promise<Result> call(Context ctx) throws Throwable"

in Action class. 在动作课上。 so delegate.call means that wrap parametes in action and return result. 因此,委托.call意味着将参数包装在动作中并返回结果。

I know Play's Action is an instance of Action, but the return value is actually CompletionStage, not Action. 我知道Play的Action是Action的一个实例,但是返回值实际上是CompletionStage,而不是Action。

CompletionStage is actually is: "public interface CompletionStage" Its documentation describes it: CompletionStage实际上是:“公共接口CompletionStage”,其文档对此进行了描述:

A stage of a possibly asynchronous computation, that performs an action or computes a value when another CompletionStage completes. 可能异步计算的阶段,当另一个CompletionStage完成时,该阶段执行操作或计算值。 A stage completes upon termination of its computation, but this may in turn trigger other dependent stages. 一个阶段在其计算终止时完成,但是这又可能触发其他相关阶段。 The functionality defined in this interface takes only a few basic forms, which expand out to a larger set of methods to capture a range of usage styles: 此接口中定义的功能仅采用几种基本形式,这些形式扩展为一大套方法,以捕获各种使用样式:

The computation performed by a stage may be expressed as a Function, Consumer, or Runnable (using methods with names including apply , accept , or run , respectively) depending on whether it requires arguments and/or produces results. 阶段执行的计算可以表示为Function,Consumer或Runnable(分别使用名称包括applyacceptrun的方法 ),具体取决于它是否需要参数和/或产生结果。 For example, {@code stage.thenApply(x -> square(x)).thenAccept(x -> System.out.print(x)).thenRun(() -> System.out.println())}. 例如,{@ code stage.thenApply(x-> square(x))。thenAccept(x-> System.out.print(x))。thenRun(()-> System.out.println())}。 An additional form ( compose ) applies functions of stages themselves, rather than their results. 附加形式( compose )应用阶段本身的功能,而不是其结果。

This 这个

"A stage of a possibly asynchronous computation, that performs an action or computes a value when another CompletionStage completes." “一个可能异步计算的阶段,当另一个CompletionStage完成时,该阶段执行一个动作或计算一个值。”

and

"The computation performed by a stage may be expressed as a Function, Consumer, or Runnable" “由阶段执行的计算可以表示为功能,使用者或可运行”

Clears that it is function, Consumer, or Runnable that performs some computation. 清除执行某些计算的是函数,使用者或Runnable。 You are actually composing actions and returning 您实际上是在编写动作并返回

return ok("It works!");

not CompletionStage. 不是CompletionStage。

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

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