Play Framework 2.4 不接受控制器 "public static Result"
Play Framework 2.4 don't accept "public static Result" for controllers
我尝试在 Mac 中使用带有 JDK8 的 Play Framework 2.4 启动一个应用程序,当我使用 ./activator new Project play-java 下载基础时,模板代码包含以下内容:
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."));
}
}
但是当我替换这部分时:
public static Result index() {...
将 "static" 添加到 index()
我收到这个错误
Compilation error
value index is not a member of controllers.Application
.../conf/routes:6
4 # ~~~~
5 # Home page
6 GET / controllers.Application.index()
我不知道为什么,因为在所有示例中,结果都使用静态
您可能仍在使用旧式路由。
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
或者,您可以坚持使用静态路由器(但如果您要创建一个新的应用程序,为什么要这么做?)并在操作引用前加上 @
GET /some/path @controllers.Application.index()
Play 2.4 将默认路由生成器更改为 InjectedRoutesGenerator 以对路由使用依赖注入。至少 tjhat 是在 play-java 模板中设置的。
如果您仍想使用静态方式,请在 build.sbt 文件
中注释以下行
// Play provides two styles of routers, one expects its actions to be injected, the
// other, legacy style, accesses its actions statically.
//routesGenerator := InjectedRoutesGenerator
有关详细信息,请参阅 https://www.playframework.com/documentation/2.4.x/Migration24 指南 > 依赖注入 > 路由
在 Play 2.5 中,默认使用注入路由。如果您仍想使用静态路由,请将此添加到您的 build.sbt:
routesGenerator := StaticRoutesGenerator
@controllers.…
符号对我不起作用。
我尝试在 Mac 中使用带有 JDK8 的 Play Framework 2.4 启动一个应用程序,当我使用 ./activator new Project play-java 下载基础时,模板代码包含以下内容:
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."));
}
}
但是当我替换这部分时:
public static Result index() {...
将 "static" 添加到 index()
我收到这个错误
Compilation error
value index is not a member of controllers.Application
.../conf/routes:6
4 # ~~~~
5 # Home page
6 GET / controllers.Application.index()
我不知道为什么,因为在所有示例中,结果都使用静态
您可能仍在使用旧式路由。
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
或者,您可以坚持使用静态路由器(但如果您要创建一个新的应用程序,为什么要这么做?)并在操作引用前加上 @
GET /some/path @controllers.Application.index()
Play 2.4 将默认路由生成器更改为 InjectedRoutesGenerator 以对路由使用依赖注入。至少 tjhat 是在 play-java 模板中设置的。 如果您仍想使用静态方式,请在 build.sbt 文件
中注释以下行// Play provides two styles of routers, one expects its actions to be injected, the
// other, legacy style, accesses its actions statically.
//routesGenerator := InjectedRoutesGenerator
有关详细信息,请参阅 https://www.playframework.com/documentation/2.4.x/Migration24 指南 > 依赖注入 > 路由
在 Play 2.5 中,默认使用注入路由。如果您仍想使用静态路由,请将此添加到您的 build.sbt:
routesGenerator := StaticRoutesGenerator
@controllers.…
符号对我不起作用。