项目/应用/ 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()
我不知道为什么因为在所有的例子中都使用了static for Result
解决方法
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()