Play Framework 2.4 does not accept "public static Result" for controllers

I am trying to run an application using Play Framework 2.4 with JDK8 on Mac when I load the database using. / activator new Project play-java, the template code contains the following:

Project / application / 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 all examples use static for Result

+6
source share
3 answers

You may still be using legacy style routing.

From the documentation :

Injected Route Generator By default, Play generates a static router, which assumes that all actions are static methods. By configuring Play to use the injected route generator, you can get Play to create a router that declares all the controllers it routes to as dependencies, allowing your controllers to be dependent injections on their own.

We recommend that you always use the generator of the entered routes, the static route generator exists primarily as a tool that helps migration, so existing projects do not need to make all their controllers unsteady at once.

To enable the generator of the entered routes, add the following build parameters to build.sbt:

routesGenerator: = InjectedRoutesGenerator

Alternatively, you can use a static router (but if you are creating a new application, why do you need it?) And an action link prefix with

 GET /some/path @controllers.Application.index() 
+10
source

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

 routesGenerator := StaticRoutesGenerator 

The designation @controllers.… does not work for me.

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

+4
source

Play 2.4 changed the default route generator to InjectedRoutesGenerator to use Injection Dependency for routes. At least this is what is created in the play-java template. If you want to still use the static path comment, the next line in the 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> Injection Dependency> Routing for details

+1
source

Source: https://habr.com/ru/post/989129/


All Articles