【问题标题】:Action Chaining Not Redirecting After Controller.await(int millis)在 Controller.await(int millis) 之后没有重定向的动作链接
【发布时间】:2011-11-11 09:10:40
【问题描述】:

我不确定这是否没有按预期工作,这只是我感到困惑。

我想做的是异步延迟一个动作,我和await(int millis)好像可以成为朋友了。

会发生什么:

1. Application.index 显示一个向Application.something 发送 POST 的表单。

app/controllers/Application.java

public static void index() {
    render();
}

app/views/Application/index.html

#{form @Application.something()}
    <input type="submit">
#{/form}

2. Application.something 完成它的工作,然后链接回 index

app/controllers/Application.java

public static void something() {
    await(500);

    // Here be business

    index();
}

3. Play 引发应用程序错误:“模板 Application/something.html 不存在”

所以当Application.index 中的Application.something 执行被暂停/恢复后在Application.index 中调用render() 时,它会尝试呈现Application.something 的模板,这当然不存在。

如果我删除 await(500) 一切正常(发出 302 并按预期呈现 index)。

我可以强制重定向

redirect("/");

得到我想要的结果,但这感觉很难看。

我也可以设置

request.action = "Application.index";

awaitApplication.index 中的渲染之后手动按预期工作(实际上,Controller.template() 中的魔法工作)。


所以基本上,一切正常吗?我不得不在await 之后使用字符串,而不是方法调用,还是有点不对劲?

干杯, 托比亚斯。

【问题讨论】:

    标签: playframework continuation async-await


    【解决方案1】:

    当你调用 await() 方法时,Play 会挂起这个 HTTP 请求。超时后,它作为一个新的 HTTPRequest 再次启动,就像被再次调用一样。

    在第一种情况下,如果没有 await() 方法,重定向会正确发生 - 这是因为该方法调用被 Play 框架正确拦截,并且路由器反向路由生成创建发出 302 重定向所需的 URL . (ActionChaining 文档中的详细信息)

    但是,在第二种情况下,在 await() 方法之后,会创建一个新的 HTTPRequest,并且不会发生任何操作链接 - 这意味着框架不会拦截对 index() 方法的调用。这与任何其他方法一样执行,因此您看不到重定向。

    【讨论】:

    • 感谢您的回答,尽管听起来您的解释是针对未使用延续的 await pre-1.2。我认为 1.2 之后等待的目的是不重新运行整个请求。 :o
    猜你喜欢
    • 2019-09-04
    • 1970-01-01
    • 1970-01-01
    • 2013-04-10
    • 1970-01-01
    • 1970-01-01
    • 2020-07-20
    • 2010-10-06
    • 1970-01-01
    相关资源
    最近更新 更多