playframework-2.0 - 使用 Play 框架 2.1.1 从子项目访问 Assets

标签 playframework-2.0

我目前正在开发 Play! 2.1.1 应用定义多个子项目。这是我的 Build.scala :

val lfamName = "lfam"
val lfamVersion = "1.0-SNAPSHOT"
val lfamDirectory = "subprojects/" + lfamName
lazy val lfam = play.Project(lfamName, lfamVersion, appDependencies, path = file(lfamDirectory))

val mainName = "main"
val mainVersion = "1.0-SNAPSHOT"
lazy val main = play.Project(mainName, mainVersion, appDependencies)
            .settings().dependsOn(lfam).aggregate(lfam)

结果结构:

app
  └ controllers
    └> Application.scala
  └ models
  └ views
    └> index.scala.html
  └ assets
    └ stylesheets
      └> main.less
conf
  └> application.conf
  └> routes
subprojects
  └ lfam
    └ conf
      └> lfam.routes
    └ app/controllers
      └> Application.scala
      └> Assets.scala
    └ app/models
    └ app/views
      └> index.scala.html
    └ assets
      └ stylesheets
        └> main.less

来源:

路线

GET     /                           controllers.Application.index

-> /lfam lfam.Routes

GET     /assets/*file               controllers.Assets.at(path="/public", file)

lfam.routes

GET     /                           controllers.lfam.Application.index

GET     /assets/*file               controllers.lfam.Assets.at(path="/public", file)

Assets .scala

package controllers.lfam
object Assets extends controllers.AssetsBuilder

Application.scala(只是包名在项目之间变化)

package controllers(.lfam)

import play.api._
import play.api.mvc._

object Application extends Controller {

  def index = Action {
    Ok(views.html.index())
  }

}

main.less(主要项目)

@main-color: #0000ff;

h1 {
  color: @main-color;
}

main.less(lfam 项目)

@main-color: #ff0000;

h1 {
  color: @main-color;
}

index.scala.html(主项目)

<!DOCTYPE HTML>
<html>
    <head>
        <link rel="stylesheet" href="@routes.Assets.at("stylesheets/main.css")">
    </head>
    <body>
        <h1>Header</h1>
    </body>
</html>

index.scala.html(lfam 项目)

<!DOCTYPE HTML>
<html>
    <head>
        <link rel="stylesheet" href="@lfam.routes.Assets.at("stylesheets/main.css")">
    </head>
    <body>
        <h1>Header</h1>
    </body>
</html>

当显示子项目索引 (/lfam/) 时,标题仍然是蓝色的。但是,如果我将 main.less 文件从子项目重命名为 lfam.less(以及相应的 html),标题将变为红色(如预期的那样)。

是否可以在多个子项目中使用相同名称的多个 Assets ?为什么 play 在第一种情况下没有为我提供正确的 css 文件,但找到了第二种情况?

谢谢

最佳答案

这是不可能的,因为 all projects in a multi-project setup use the same classloader .

使用 ClassLoader.getResource 加载资源 (source) .就像 Java 源代码一样,完全限定的资源名称必须是唯一的。

关于playframework-2.0 - 使用 Play 框架 2.1.1 从子项目访问 Assets ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16748937/

相关文章:

java - Play Framework 2 slugify

playframework - 无法找到 play.filters 包

java - 使用 Ebean 在 Play Framework 2.3.3 中映射 @ManyToOne 关系的异常

playframework-2.0 - 如何设置 Play Framework 2.0 的 javac 编译版本以防止 "Unsupported major.minor version"?

playframework-2.0 - Play 框架 2.0.2 SQL Server 配置

java - 下载的文件是 html View ,而不是带有 Play Framework 的 CSV

java - 如何接收 Play 2.0 调试邮件?

scala - Play 2.0/SBT : Exclude certain transitive dependencies from some/all modules in Build. 斯卡拉

java - 将 Gson 添加到 Play2 框架

Java 玩吧! 2 - 密码限制