【问题标题】:Play Framework 2.8 - request and flash not found on viewPlay Framework 2.8 - 在视图中找不到请求和闪存
【发布时间】:2020-05-03 03:11:26
【问题描述】:

我正在尝试获取从视图中的控制器传递过来的闪存数据。在 Play 文档 (https://www.playframework.com/documentation/2.8.x/JavaSessionFlash) 中,它说闪存可以是这样的:

@flash.get("success").orElse("Welcome!")

但是,当我尝试编译项目时遇到此错误

[error] .../edit.scala.html:4:3: not found: value flash
[error]     @flash.get("test").orElse("No flash!")
[error]      ^
[error] one error found
[error] (Compile / compileIncremental) Compilation failed

我也尝试过使用@request.flash,它给出了同样的错误。

如何在 Play Framework 2.8 的视图中获取 flash 或 request。我错过了一些进口或类似的东西吗?

【问题讨论】:

  • 您是否将隐式 flash 参数添加到模板中?

标签: java web playframework


【解决方案1】:

由于您使用的是 Java,因此您需要将请求参数传递给模板。在模板文件的顶部:

@( ... your normal params ...)(implicit request: Http.Request)

现在使用请求获取闪存:

@request.flash().getOptional("test")

【讨论】:

  • 您好@vm4,感谢您的回答。我尝试这样做,但现在控制器中出现错误,因为它期望在操作结果中传递 request 参数。我收到此错误:.../RecipeController.java:52:1: method render in class views.html.recipe.edit cannot be applied to given types; [error] required: models.Recipe,play.mvc.Http.Request [error] found: models.Recipe [error] reason: actual and formal argument lists differ in length [error] return ok(views.html.recipe.edit.render(recipe)).flashing("danger", "Error");
  • 那么您需要将请求参数传递给模板。像这样声明你的控制器方法:playframework.com/documentation/2.8.x/…?
  • 知道了。我认为使用implicit 不需要在结果中显式返回请求。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-29
  • 1970-01-01
  • 1970-01-01
  • 2020-06-27
  • 2017-07-28
  • 1970-01-01
相关资源
最近更新 更多