PlayFramework初学者。路线定义错误

时间:2017-01-27 11:25:24

标签: java playframework playframework-2.0

我制作了一个简单的新控制器并试图定义我的路线。一切似乎都是正确的,但我得到一个错误。该代码取自Manning Play for Java。

产品控制器:

package controllers;

import play.mvc.*;
import play.mvc.Controller;
import play.mvc.Result;

public class Products extends Controller {

//list all products
public static Result list(){
    return TODO;
}

//return empty form for adding
public static Result newProduct(){
    return TODO;
}

//product edit form
public static Result details(String ean){
    return TODO;
}

//save a product
public static Result save(){
    return TODO;
}

}

路线:

GET     /                           controllers.HomeController.index

GET     /count                      controllers.CountController.count

GET     /message                    controllers.AsyncController.message


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

GET     /products                   controllers.Products.list
GET     /products/new               controllers.Products.newProduct
GET     /products/:ean              controllers.Products.details(ean: String)
POST    /products/                  controllers.Products.save

错误:

Compiling 6 Scala sources and 11 Java sources to /Users/andrei/Desktop/PlayFramework/target/scala-2.11/classes...
[error] /Users/andrei/Desktop/PlayFramework/conf/routes:15: value list is not a member of controllers.Products
[error] GET     /products                   controllers.Products.list
[error] /Users/andrei/Desktop/PlayFramework/conf/routes:16: value newProduct is not a member of controllers.Products
[error] GET     /products/new               controllers.Products.newProduct
[error] /Users/andrei/Desktop/PlayFramework/conf/routes:17: value details is not a member of controllers.Products
[error] GET     /products/:ean              controllers.Products.details(ean: String)
[error] /Users/andrei/Desktop/PlayFramework/conf/routes:18: value save is not a member of controllers.Products
[error] POST    /products/                  controllers.Products.save
[error] four errors found

1 个答案:

答案 0 :(得分:1)

从版本2.5开始,Play开始使用InjectedRoutesGenerator,这将禁止static控制器方法。因此,简单的解决方案是删除static关键字。

但是,如果你真的想拥有静态方法(我不明白为什么),你可以使用遗留(2.5.0之前的)static路由生成器,假设所有动作都是{{ 1}} 方法

  

您可以将Play配置为使用旧版(2.5.0之前版本)静态路由   生成器,假定所有操作都是静态方法。至   配置项目,将以下内容添加到build.sbt:

     

routesGenerator:= StaticRoutesGenerator我们建议始终使用   注入路线发电机。静态路由生成器存在   主要作为一种辅助迁移的工具,以便现有项目不会   必须立即使所有控制器不是静态的。

     

如果使用静态路由生成器,则可以指示该操作   有一个注入的控制器,通过操作前缀为@,如下所示:

     

GET / some / path @ controllers.Application.index()