user7120361
user7120361

Reputation:

PlayFramework beginner. Route definition error

I have a made a simple new controller and tried to define my routes. Everything seems to be correct but I get an error. The code is taken from the Manning Play for Java.

Products Controller:

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;
}

}

Routes:

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

Errors:

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

Upvotes: 2

Views: 84

Answers (1)

Mysterion
Mysterion

Reputation: 9320

Since version 2.5, Play starts using InjectedRoutesGenerator, which will prohibit static controllers methods. So, simple solution will be to remove static keyword.

But, if you really want to have static methods (I don't see why), you could user legacy (pre 2.5.0) static routes generator, that assumes that all actions are static methods

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()

Upvotes: 1

Related Questions