我制作了一个简单的新 Controller 并尝试定义我的路线。一切似乎都是正确的,但我得到了一个错误。代码取自 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
最佳答案
从 2.5 版开始,Play 开始使用 InjectedRoutesGenerator
,这将禁止 static
Controller 方法。因此,简单的解决方案是删除 static
关键字。
但是,如果您真的想要静态方法(我不明白为什么),您可以使用旧版(2.5.0 之前)static
路由生成器,它假定所有操作都是 静态
方法
You can configure Play to use the legacy (pre 2.5.0) static routes generator, that assumes that all actions are static methods. To configure the project, add the following to build.sbt:
routesGenerator := StaticRoutesGenerator We recommend always using the injected routes generator. The static routes generator exists primarily as a tool to aid migration so that existing projects don’t have to make all their controllers non static at once.
If using the static routes generator, you can indicate that an action has an injected controller by prefixing the action with @, like so:
GET /some/path @controllers.Application.index()
关于java - PlayFramework 初学者。路由定义错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41892892/