【问题标题】:Play Framework config value in view在视图中播放框架配置值
【发布时间】:2011-10-10 18:18:48
【问题描述】:

如何在视图中从conf/application.conf 访问值application.name

【问题讨论】:

标签: playframework


【解决方案1】:

您可以使用以下代码示例:

${play.configuration['application.name']}

另见http://groups.google.com/group/play-framework/browse_thread/thread/1412ca8fc3edd22f

【讨论】:

【解决方案2】:

Play 2.5.x 更新

在 Play Scala 2.5.x 中,对象 Play 中的当前方法已被弃用。为了从 conf/application.conf 中读取值,您必须改用 DI。

在你的控制器中注入play.api.Configuration

class MyController @Inject() (val configuration: play.api.Configuration) extends Controller

然后,您可以直接在方法中使用configuration

def sayMyName = Action { request =>
   Ok("Your name is " + configuration.getString("application.name"))
}

您也可以在您的视图旋转模板中使用configuration

def sayMyNameUsingView = Action { request =>
   implicit lazy val config = configuration
   Ok(views.html.index())
}

将注入的configuration 隐式发送到给定的index.scala.html

@()(implicit val configuration:play.api.Configuration)
<html>
  <body>
    <h1>Your name is @configuration.getString("application.name")</h1> 
  </body>
</html>

【讨论】:

    【解决方案3】:

    Play 2 更新...

    在 Play Scala 2.3.x 中,要从 conf/application.conf 读取值,您可以执行以下操作:

    import play.api.Play.current
    ...
    current.configuration.getString("application.name")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-12
      • 2011-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多