4

我需要为我的 Play 应用程序配置一些 URL,所以我将它们添加到application.conf

application.url="http://www.mydomain.com"
application.url.images="http://www.anotherdomain.com"
application.url.images.logo="${application.url.images}/logo.png"
...

下面是我在视图中用于访问上述条目的代码:

@(title: String)
@import play.api.Play.current

<!DOCTYPE html>

<html>
    ...

    <img src="@{ currrent.configuration.getString("application.url.images.logo") }" />

    ...
</html>

嗯......我快疯了,因为每当我运行应用程序时,我总是收到以下错误消息:

/home/j3d/Projects/test-app/conf/application.conf: 14-19: application.url.images.logo has type OBJECT rather than STRING

任何的想法?我错过了什么吗?或者它是一个错误?

非常感谢你。

4

1 回答 1

14

Play 中使用的Typesafe 配置库中的Config表示类似 JSON 的结构。点表示法是创建嵌套对象({ ... }在 JSON 中)的语法糖。例如:

application.url="http://example.com"
application.images.logo="http://example.com/img/1.png"
application.images.header="http://example.com/img/3.png"

相当于以下 JSON:

{
  "application": {
    "url": "http://example.com",
    "images": {
      "logo": "http://example.com/img/1.png",
      "header": "http://example.com/img/3.png"
    }
  }
}

在您的示例中,您首先将字符串分配给application.url,然后尝试向其添加键(键urlin application.url.images),就像它是 JSON 对象,而不是字符串。在这种情况下,我不知道 Typesafe Config 的确切行为,以及为什么它在读取配置文件时不会立即引发错误。

尝试重新排列配置键的层次结构,即:

application.url.prefix="http://www.mydomain.com"
application.url.images.prefix="http://www.anotherdomain.com"
application.url.images.logo="${application.url.images}/logo.png"

这里application.url将是带有键prefix和的对象images,并且application.url.images将是带有键prefix和的对象logo

于 2013-02-12T08:24:34.863 回答