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

前端之家收集整理的这篇文章主要介绍了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()
原文链接:https://www.f2er.com/java/126749.html

猜你在找的Java相关文章