我是 PlayFramework 的新手。
请给我一个如何从我的角度访问配置参数的示例。我正在使用 PlayFramework 2.5.3
旧方式(@current 已弃用):
@current.configuration.getString("environment.param")
新方式(据我所知,应该注入(inject)配置):
我知道如何从 Controller 访问它。
@Inject() (val messagesApi: MessagesApi, configuration: Configuration)
从我的角度来看,我如何使用它?
最佳答案
遗憾的是,您对此无能为力。这就是将 DI 引入 Play 时的情况,关于模板的讨论不多。一种可能的解决方案是:
Configuration
implicit
发送到您的 View /模板class Application @Inject() (implicit val config: Configuration) extends Controller {
def index = Action {
Ok(views.html.index("foo"))
}
}
您的模板将如下所示:
@(myParam1: Any)(implicit config: Configuration)
<h2>Some HTML here @myParam1 @config.getString("environment.param")</h2>
我完全意识到这在某种程度上违背了 DI 的目的,但这就是现在的情况。
关于scala - 玩 2.5.X : method current in object Play is deprecated: This is a static reference to application, 改用 DI,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36955237/