Scala播放框架:编译错误[类型应用程序不是包控制器的成员]

Scala播放框架:编译错误[类型应用程序不是包控制器的成员],scala,playframework,playframework-2.0,Scala,Playframework,Playframework 2.0,我试图在书中编译一个scala示例:Play for scala,但在Play console上出现以下编译错误: C:\Play\exp\ch6_implicits>activator run [info] Loading project definition from C:\Play\exp\ch6_implicits\project [info] Set current project to ch6_implicits (in build file:/C:/Play/exp/ch6_

我试图在书中编译一个scala示例:Play for scala,但在Play console上出现以下编译错误:

C:\Play\exp\ch6_implicits>activator run
[info] Loading project definition from C:\Play\exp\ch6_implicits\project
[info] Set current project to ch6_implicits (in build file:/C:/Play/exp/ch6_implicits/)

--- (Running the application, auto-reloading is enabled) ---

[info] p.a.l.c.ActorSystemProvider - Starting application default Akka system: application
[info] p.c.s.NettyServer$ - Listening for HTTP on /0:0:0:0:0:0:0:0:9000

(Server started, use Ctrl+D to stop and go back to the console...)

[info] Compiling 13 Scala sources and 1 Java source to C:\Play\exp\ch6_implicits\target\scala-2.11\classes...
[error] C:\Play\exp\ch6_implicits\conf\routes:6: type Application is not a member of package controllers
[error] GET             /                               controllers.Application.index
[error] C:\Play\exp\ch6_implicits\conf\routes:7: type Shop is not a member of package controllers
[error] GET             /catalog                controllers.Shop.catalog
[error] C:\Play\exp\ch6_implicits\conf\routes:6: type Application is not a member of package controllers
[error] GET             /                               controllers.Application.index
[error] C:\Play\exp\ch6_implicits\conf\routes:7: type Shop is not a member of package controllers
[error] GET             /catalog                controllers.Shop.catalog
[error] C:\Play\exp\ch6_implicits\conf\routes:6: type Application is not a member of package controllers
[error] GET             /                               controllers.Application.index
[error] C:\Play\exp\ch6_implicits\conf\routes:7: type Shop is not a member of package controllers
[error] GET             /catalog                controllers.Shop.catalog
[error] 6 errors found
[error] (compile:compileIncremental) Compilation failed
[error] application -

! @6mafk6inc - Internal server error, for (GET) [/products/new] ->

play.sbt.PlayExceptions$CompilationException: Compilation error[type Application is not a member of package controllers]
        at play.sbt.PlayExceptions$CompilationException$.apply(PlayExceptions.scala:27) ~[na:na]
        at play.sbt.PlayExceptions$CompilationException$.apply(PlayExceptions.scala:27) ~[na:na]
        at scala.Option.map(Option.scala:145) ~[scala-library-2.11.6.jar:na]
        at play.sbt.run.PlayReload$$anonfun$taskFailureHandler$1.apply(PlayReload.scala:49) ~[na:na]
        at play.sbt.run.PlayReload$$anonfun$taskFailureHandler$1.apply(PlayReload.scala:44) ~[na:na]
        at scala.Option.map(Option.scala:145) ~[scala-library-2.11.6.jar:na]
        at play.sbt.run.PlayReload$.taskFailureHandler(PlayReload.scala:44) ~[na:na]
        at play.sbt.run.PlayReload$.compileFailure(PlayReload.scala:40) ~[na:na]
        at play.sbt.run.PlayReload$$anonfun$compile$1.apply(PlayReload.scala:17) ~[na:na]
        at play.sbt.run.PlayReload$$anonfun$compile$1.apply(PlayReload.scala:17) ~[na:na]
Application.scala是:

package controllers

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

// We extend the 'WithCart' trait, so we have an implicit conversion from RequestHeader to Cart
object Application extends Controller with WithCart {

  def index = Action { implicit request =>
    // The index template takes an implicit Cart, which is not available.
    // However, the WithCart trait has an implicit conversion from
    // RequestHeader to Cart, and we do have an implicit RequestHeader
    // here, because `request` is a Request, which extends RequestHeader.
    Ok(views.html.index())
  }

  def contact = Action { implicit request =>
    Ok(views.html.contact())
  }

}       
斯卡拉商店

package controllers

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

object Shop extends Controller with WithCart {

  def catalog() = Action { implicit request =>
    val products = ProductDAO.list
    Ok(views.html.products.catalog(products))
  }

}
文件是:

# Routes
# This file defines all application routes (Higher priority routes first)
# ~~~~

# Home page
GET     /               controllers.Application.index
GET     /catalog        controllers.Shop.catalog
GET     /contact        controllers.Application.contact

# Map static resources from the /public folder to the /assets URL path
GET     /assets/*file               controllers.Assets.at(path="/public", file)
Play Framework是2.4.0,大约说:

[ch6_implicits] $ about
[info] This is sbt 0.13.8
[info] The current project is {file:/C:/Play/exp/ch6_implicits/}root 1.0-SNAPSHOT
[info] The current project is built against Scala 2.11.6
[info] Available Plugins: sbt.plugins.IvyPlugin, sbt.plugins.JvmPlugin, sbt.plugins.CorePlugin, sbt.plugins.JUnitXmlReportPlugin, play.sbt.Play, play.sbt.PlayAkkaHttpServer, play.sbt.PlayJava, play.sb
t.PlayLayoutPlugin, play.sbt.PlayNettyServer, play.sbt.PlayScala, play.sbt.routes.RoutesCompiler, play.twirl.sbt.SbtTwirl, com.typesafe.sbt.SbtNativePackager, com.typesafe.sbt.packager.archetypes.Akka
AppPackaging, com.typesafe.sbt.packager.archetypes.JavaAppPackaging, com.typesafe.sbt.packager.archetypes.JavaServerAppPackaging, com.typesafe.sbt.packager.archetypes.jar.ClasspathJarPlugin, com.types
afe.sbt.packager.archetypes.jar.LauncherJarPlugin, com.typesafe.sbt.packager.debian.DebianDeployPlugin, com.typesafe.sbt.packager.debian.DebianPlugin, com.typesafe.sbt.packager.debian.JDebPackaging, c
om.typesafe.sbt.packager.docker.DockerPlugin, com.typesafe.sbt.packager.jdkpackager.JDKPackagerDeployPlugin, com.typesafe.sbt.packager.jdkpackager.JDKPackagerPlugin, com.typesafe.sbt.packager.linux.Li
nuxPlugin, com.typesafe.sbt.packager.rpm.RpmDeployPlugin, com.typesafe.sbt.packager.rpm.RpmPlugin, com.typesafe.sbt.packager.universal.UniversalDeployPlugin, com.typesafe.sbt.packager.universal.Univer
salPlugin, com.typesafe.sbt.packager.windows.WindowsDeployPlugin, com.typesafe.sbt.packager.windows.WindowsPlugin, com.typesafe.sbt.web.SbtWeb, com.typesafe.sbt.jse.SbtJsEngine, com.typesafe.sbt.jse.S
btJsTask, com.typesafe.sbt.coffeescript.SbtCoffeeScript, com.typesafe.sbt.less.SbtLess, com.typesafe.sbt.jshint.SbtJSHint, com.typesafe.sbt.rjs.SbtRjs, com.typesafe.sbt.digest.SbtDigest, com.typesafe.
sbt.mocha.SbtMocha, com.typesafe.sbteclipse.plugin.EclipsePlugin
[info] sbt, sbt plugins, and build definitions are using Scala 2.10.4
[ch6_implicits] $
Java版本为:

C:\Play\exp\ch6_implicits>java -version
java version "1.8.0_45"
Java(TM) SE Runtime Environment (build 1.8.0_45-b15)
Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)

C:\Play\exp\ch6_implicits>javac -version
javac 1.8.0_45
你知道问题出在哪里,怎么解决吗

到目前为止,我的环境中已经编译并运行了其他示例,没有任何错误


谢谢你的支持

默认情况下,Play 2.4生成依赖注入路由器,这与以前使用静态路由器时不同。您有两个选项,从build.sbt中删除routesGenerator行,这样play将生成一个静态路由器,或者最好使您的控制器类而不是对象,并使用依赖项注入。

从您的文件build.sbt中删除routesGenerator:=InjectedRoutesGenerator,所有选项都将起作用。

将您的对象更改为如下类:

object Shop extends Controller with WithCart {
class Shop extends Controller with WithCart {

object Application extends Controller with WithCart {
class Application extends Controller with WithCart {

在conf/application.conf文件中包含以下代码:


play.allowglobalaapplication=true

这又是一个突破性变化背后的理念是什么?实际上,这不是一个突破性变化,因为它只是新项目的默认值-现有项目使用静态路由器,并且必须明确选择DI路由器。其原理是全局状态非常糟糕,它使测试变得困难,并且使您更难对应用程序进行推理。静态路由器强制全局状态。一个自包含的简单示例将是最受欢迎的,我没有投票,因为现在我必须在其他地方查找如何进行控制器注入,所以这肯定值得提出一个自己的问题:现在我们没有全局状态有那么糟糕吗?每次我们提出请求时,我的演员似乎都被重新塑造了。有没有办法保持控制器内的状态?若要注入控制器,请在路由器中的函数定义前面添加“@”,并使控制器成为类。例如:GET/index@controllers.Application.index