Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play Framework 2.4 don't accept "public static Result" for controllers

I am try to start an app using Play Framework 2.4 with JDK8 in Mac, when I download the base using ./activator new Project play-java the template code contains the next:

Project/app/controlles/Application.java

package controllers;

import play.*;
import play.mvc.*;

import views.html.*;

public class Application extends Controller {

    public Result index() {
        return ok(index.render("Your new application is ready."));
    }

}

But when I replace this part:

public static Result index() {...

adding "static" to index()

I get this error

Compilation error
value index is not a member of controllers.Application
.../conf/routes:6
4 # ~~~~
5 # Home page
6 GET     /                           controllers.Application.index()

I don't know why because in all examples used static for Result

like image 825
Alejandro Quintanar Avatar asked Jun 15 '15 11:06

Alejandro Quintanar


3 Answers

You're probably still using legacy-style routing.

From the documentation:

Injected routes generator By default, Play will generate a static router, that assumes that all actions are static methods. By configuring Play to use the injected routes generator, you can get Play to generate a router that will declare all the controllers that it routes to as dependencies, allowing your controllers to be dependency injected themselves.

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.

To enable the injected routes generator, add the following to your build settings in build.sbt:

routesGenerator := InjectedRoutesGenerator

Alternatively, you can stick with the static router (but if you're creating a new app, why would you?) and prefix the action reference with a @

GET        /some/path        @controllers.Application.index()
like image 82
Steve Chaloner Avatar answered Oct 15 '22 01:10

Steve Chaloner


In Play 2.5 injected routes are used by default. If you still want to use static routes add this to your build.sbt:

routesGenerator := StaticRoutesGenerator

The @controllers.… notation didn't work for me.

More details here: https://playframework.com/documentation/2.5.x/Migration25#Routes-generated-with-InjectedRoutesGenerator

like image 6
stare.in.the.air Avatar answered Oct 15 '22 01:10

stare.in.the.air


Play 2.4 changed the default routes generator to InjectedRoutesGenerator to use Dependency Injection for routes. At least tjhat is what is set up in the play-java template. If you want to still use the static way comment the following line in your build.sbt file

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

See https://www.playframework.com/documentation/2.4.x/Migration24 guide > Dependency Injection > Routing for details

like image 1
Alexander B Avatar answered Oct 15 '22 00:10

Alexander B