由于构建或路由导致 Play Framework 2.4 编译错误

Posted

技术标签:

【中文标题】由于构建或路由导致 Play Framework 2.4 编译错误【英文标题】:Play Framwork 2.4 compile error due to build or routing 【发布时间】:2015-11-08 20:35:15 【问题描述】:

今天,一边用 Play! 做教程!框架,我在编译源码的时候遇到了一个问题,貌似和路由有关。

我有一个朋友,一个非常有经验的专业程序员(也有玩 Play 的经验),研究了它,当我们研究它时,他自己的个人项目在 Mac OS 上运行时也出现了同样的问题。

我在运行 Ubuntu 15.10 的个人计算机以及运行 Fedora 23 的 Digitalocean Droplet 上尝试了以下步骤。

这是我的步骤:

    activator new (选择“play-java”作为模板,选择“shop”作为项目名称。

2.) 将以下“Items.java”添加到 app/controllers-directory 中:

package controllers;

import play.mvc.Controller;
import play.mvc.Result;
import play.mvc.Results;

public class Items extends Controller 

        static final Shop shop = Shop.Shop;

        public static Result list() 
                return Results.ok();
        

        public static Result create() 
                return status(NOT_IMPLEMENTED);
        

        public static Result details(Long id) 
                return status(NOT_IMPLEMENTED);
        

        public static Result update(Long id) 
                return status(NOT_IMPLEMENTED);
        

        public static Result delete(Long id) 
                return status(NOT_IMPLEMENTED);
        


conf/routes 如下所示:

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

# Home page
GET           /                    controllers.Application.index

GET           /items               controllers.Items.list


POST          /items               controllers.Items.create()
GET           /items/:id           controllers.Items.details(id: Long)
PUT           /items/:id           controllers.Items.update(id: Long)
DELETE        /items/:id           controllers.Items.delete(id: Long)


# Map static resources from the /public folder to the /assets URL path
GET           /assets/*file        controllers.Assets.versioned(path="/public", file: Asset)

build.sbt 如下:

name := """shop"""

version := "1.0-SNAPSHOT"

lazy val root = (project in file(".")).enablePlugins(PlayJava)

scalaVersion := "2.11.6"

libraryDependencies ++= Seq(
  javaJdbc,
  cache,
  javaWs
)

// Play provides two styles of routers, one expects its actions to be injected, the
// other, legacy style, accesses its actions statically.
//routesGenerator := InjectedRoutesGenerator

如果我们现在运行“activator ~run”,我会收到以下错误:

[root@sandbox shop]# ./activator ~run
[info] Loading project definition from /root/activator-dist-1.3.6/shop/project
[info] Set current project to shop (in build file:/root/activator-dist-1.3.6/shop/)

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

[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 6 Scala sources and 3 Java sources to /root/activator-dist-1.3.6/shop/target/scala-2.11/classes...
[error] /root/activator-dist-1.3.6/shop/conf/routes:6: value index is not a member of object controllers.Application
[error] GET           /                    controllers.Application.index
[error] one error found
[error] (compile:compileIncremental) Compilation failed

我们找不到解决办法,也没有找到合适的解决方案。有人有想法吗?

除了提到的 3 个文件外,我没有更改默认项目中的任何内容。这里有人知道出了什么问题吗?

【问题讨论】:

似乎索引函数不是应用程序类的一部分。首先检查是否确实有一个 Application 类。在最后一种情况下,只需删除路由文件的第一个条目并检查它是否有效。 【参考方案1】:

    在你的 build.sbt 中激活这一行:

    // Play provides two styles of routers, one expects its actions to be injected, the
    // other, legacy style, accesses its actions statically.
    routesGenerator := InjectedRoutesGenerator
    

    从您的方法中删除“静态”修饰符,例如

    public Result list() 
            return Results.ok();
    
    

描述见:https://www.playframework.com/documentation/2.4.x/Migration24 -> 依赖注入 -> 路由

【讨论】:

以上是关于由于构建或路由导致 Play Framework 2.4 编译错误的主要内容,如果未能解决你的问题,请参考以下文章

Play Framework 路由中的变量

Play Framework & IntelliJ - 无法解析符号索引/路由

Play Framework 2.5,到 Web 套接字的路由无法编译

Play Framework - 使用默认查询参数的路由(如果不存在)

如何构建版本独立的 Play!Framework 2 模块?

Play Framework [2.4.x] - 模块路由特定名称因“资产不是包的成员”而失败