java – Play Framework 2.4不接受控制器的“公共静态结果”

我尝试使用Play Framework 2.4和Mac中的JDK8启动应用程序,当我使用./activator下载基础时新建项目播放- java模板代码包含下一个

项目/应用/ 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

解决方法

您可能仍在使用旧式路由.

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

或者,你可以坚持使用静态路由器(但如果你正在创建一个新的应用程序,为什么会这样?)并在动作参考前加上@

GET        /some/path        @controllers.Application.index()

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...