gpt4 book ai didi

java - Play Framework 2.4 不接受 Controller 的 "public static Result"

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:45:34 24 4
gpt4 key购买 nike

我尝试在 Mac 中使用 Play Framework 2.4 和 JDK8 启动应用程序,当我使用 ./activator new Project play-java 下载基础时,模板代码包含下一个:

项目/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() {...

向 index() 添加“静态”

我收到这个错误

Compilation error
value index is not a member of controllers.Application
.../conf/routes:6
4 # ~~~~
5 # Home page
6 GET / controllers.Application.index()

我不知道为什么,因为在所有示例中,结果都使用静态

最佳答案

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

来自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()

关于java - Play Framework 2.4 不接受 Controller 的 "public static Result",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30844605/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com