首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PlayFramework多项目中的自定义模板

PlayFramework多项目中的自定义模板
EN

Stack Overflow用户
提问于 2017-07-25 01:04:52
回答 2查看 151关注 0票数 0

我有一个带有play模块和其他scala模块的多模块play应用程序,一切都运行得很好。我想添加一个自定义的Twirl模板,这就是出现问题的时候。这是Multiproject structure

build.sbt:

代码语言:javascript
复制
name := """scalaplay"""

version := "1.0-SNAPSHOT"

lazy val root = (project in file(".")).enablePlugins(PlayScala).dependsOn(restfulapi,util).aggregate(restfulapi,util)
scalaVersion := "2.11.7"

/**
  * .dependsOn(util). will let us use element from dbmodule into apirestmodule. Specifically some element and structure
  * of the data model.
  *
  */

lazy val restfulapi = (project in file("modules/apirest")).enablePlugins(PlayScala).dependsOn(util).settings(scalaVersion:="2.11.7",
  libraryDependencies ++= Seq(
    cache,
    "org.scalatestplus.play" %% "scalatestplus-play" % "1.5.1" % Test
  )
)

lazy val util = (project in file("modules/dbmodule")).settings(scalaVersion:="2.11.7")

TwirlKeys.templateFormats += ("csv" -> "views.CsvFormat")

Apirest.routes的一部分:

代码语言:javascript
复制
    #processing premierLeague
    POST     /premier/match             controllers.PremierleagueController.insertMatch

    GET      /premier/matchs            controllers.PremierleagueController.getMatchGame

    GET     /assets/*file            controllers.Assets.versioned(path="/public", file: Asset)

    GET     /records                    controllers.HomeController.records

使用该模板的操作位于HomeController.scala中:

代码语言:javascript
复制
......

 def records = Action {
    Ok(views.csv.records(Record.sampleRecords))
  }

.....

这是我展示我的源代码时的结果:

代码语言:javascript
复制
[scalaplay] $ show twirlCompileTemplates::sourceDirectories
[info] restfulapi/compile:twirlCompileTemplates::sourceDirectories
[info]  List(/Users/ldipotet/scala/scalaplay/modules/apirest/app)
[info] root/compile:twirlCompileTemplates::sourceDirectories
[info]  List(/Users/ldipotet/scala/scalaplay/app)

这是我在尝试编译项目时出现的编译错误:

代码语言:javascript
复制
[info] Compiling 22 Scala sources and 1 Java source to /Users/ldipotet/scala/scalaplay/modules/apirest/target/scala-2.11/classes...
[error] /Users/ldipotet/scala/scalaplay/modules/apirest/app/controllers/HomeController.scala:72: object csv is not a member of package views
[error]     Ok(views.csv.records(Record.sampleRecords))
[error]              ^
[error] one error found
[error] (restfulapi/compile:compileIncremental) Compilation failed
[error] Total time: 6 s, completed 24-jul-2017 17:18:11

如需了解更多信息,请在单个playframework项目中编译和运行相同的自定义模板

EN

回答 2

Stack Overflow用户

发布于 2017-07-27 17:29:53

问题在于对自定义格式化文件的引用:

TwirlKeys.templateFormats += ("csv“-> "views.CsvFormat")

它位于多项目根目录下的唯一built.sbt文件中。

代码语言:javascript
复制
-rw-r--r--   1 ldipotet  staff     695 27 jul 01:23 build.sbt
drwxr-xr-x   5 ldipotet  staff     170 27 jul 01:16 conf
-rw-r--r--   1 ldipotet  staff  895312 18 jul 10:44 football.txt
drwxr-xr-x   3 ldipotet  staff     102 27 jul 01:43 logs
drwxr-xr-x   5 ldipotet  staff     170 12 jul 12:28 modules
-rw-r--r--   1 ldipotet  staff     191 20 jul 13:36 package.txt
drwxr-xr-x   6 ldipotet  staff     204 27 jul 01:16 project
drwxr-xr-x   5 ldipotet  staff     170  1 jun 12:24 public
-rw-r--r--   1 ldipotet  staff  175256 12 jul 16:54 regex.png
drwxr-xr-x  11 ldipotet  staff     374 27 jul 01:43 target
drwxr-xr-x   4 ldipotet  staff     136  5 jul 09:22 test

这是错误的,因为编译器试图在模板所在的项目中查找格式。我的意思是,在项目本身(basedir/modules/api rest)中,模板在哪里,那里没有任何引用。

解决方案:在创建自定义模板的子项目basedir/modules/apirest中创建build.sbt:

basedir/modules/apirest/built.sbt

代码语言:javascript
复制
name := """apirest"""

TwirlKeys.templateFormats += ("csv" -> "views.CsvFormat")

当然,还要从根项目的built.sbt中删除TwirlKeys.templateFormats += ("csv“-> "views.CsvFormat")

无论是单项目还是多项目,引用都必须放在自定义模板所在的项目中。

票数 0
EN

Stack Overflow用户

发布于 2017-07-27 22:36:19

@ldipotet提供的Answen实际上解决了这个问题,但也有其他方法。

1只需将TwirlKeys.templateFormats += ("csv" -> "views.CsvFormat")移动到主build.sbt中的restfulapi定义中,如下所示:

代码语言:javascript
复制
...
lazy val restfulapi = (project in file("modules/apirest")).enablePlugins(PlayScala).dependsOn(util).settings(scalaVersion:="2.11.7",
  libraryDependencies ++= Seq(
    cache,
    "org.scalatestplus.play" %% "scalatestplus-play" % "1.5.1" % Test
  ),
  TwirlKeys.templateFormats += ("csv" -> "views.CsvFormat")
)
...

2如果您需要在其他子项目中使用自定义格式,请将常用设置移动到basedir/project目录中新Common.scala文件(可随意命名)中的对象:

代码语言:javascript
复制
import sbt._

object Common {
  val settings: Seq[Setting[_]] = Seq(
    TwirlKeys.templateFormats += ("csv" -> "views.CsvFormat")
  )
}

然后在子项目build.sbt中使用公共对象

代码语言:javascript
复制
name := """apirest"""

Common.settings

有关更多详细信息,请参阅子项目的PlaySBT文档。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45286250

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档