java 无法解析 IntelliJ 中的反向路由方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16676311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 23:38:18  来源:igfitidea点击:

Unable to resolve reverse routing methods in IntelliJ

javaintellij-ideaplayframework-2.0

提问by Conor Pender

I'm following one of the play framework tutorials, but I'm getting compile errors whenever I try to use reverse routing. Firstly,

我正在关注播放框架教程之一,但是每当我尝试使用反向路由时,都会遇到编译错误。第一,

public static Result javascriptRoutes() {
    response().setContentType("text/javascript");
    return ok(
        Routes.javascriptRouter("jsRoutes",
            controllers.routes.javascript.Projects.add(),
            controllers.routes.javascript.Projects.delete(),
            controllers.routes.javascript.Projects.rename(),
            controllers.routes.javascript.Projects.addGroup()
        )
    );
}

where the error shown in intelliJ is 'cannot resolve method javascriptRouter(java.lang.String, ?, ?, ?, ?)'

intelliJ 中显示的错误是“无法解析方法 javascriptRouter(java.lang.String, ?, ?, ?, ?)”

But also in the a unit test:

而且在一个单元测试中:

@Test
public void notAuthenticated() {
    Result result = callAction(
            controllers.routes.ref.Application.index(),
            fakeRequest()
    );
    assertEquals(303, status(result));
    assertEquals("/login", header("Location", result));
}

where it cannot resolve the index method.

它无法解析索引方法。

Is this a problem with intelliJ, or am I missing something within play?

这是 intelliJ 的问题,还是我在游戏中遗漏了什么?

For the first part, here is the entry in my routes file:

对于第一部分,这是我的路由文件中的条目:

GET     /assets/javascripts/routes  controllers.Application.javascriptRoutes()

and my controller, Projects, has got the defined methods.

我的控制器 Projects 已经获得了定义的方法。

回答by darren rose

File-> Project Structure

文件->项目结构

Select Sourcesin Right Pane

在右窗格中选择

Add Source folder

添加源文件夹

target/scala-XXX/classes_managed

目标/scala-XXX/classes_managed

target/scala-XXX/src_managed/main

目标/scala-XXX/src_managed/main

回答by Markus Knittig

I was running into the same problem and found the solution here: https://github.com/playframework/Play20/issues/969

我遇到了同样的问题,并在这里找到了解决方案:https: //github.com/playframework/Play20/issues/969

In short:

简而言之:

  • Create the directories javascriptand refunder the controllers package
  • Run activator compileand now Intellij should get it // used to be 'play compile'
  • If you still got the errors try to run activator ideaagain // used to be 'play compile'*
  • 创建目录javascriptref控制器包下
  • 运行activator compile,现在 Intellij 应该得到它 // 曾经是“播放编译”
  • 如果您仍然遇到错误,请尝试activator idea再次运行// 以前是“播放编译”*

回答by user2821144

Pulled from a link provided by @Markus Kittig. Great temporary fix. https://github.com/playframework/playframework/issues/1784#issuecomment-26345523

从@Markus Kittig 提供的链接中提取。伟大的临时修复。https://github.com/playframework/playframework/issues/1784#issuecomment-26345523

Synopsis: Add target/scala-XXX as a managed source and remove the app controllers and views sources flag inside File->Project Structure->Modules->Sources. Then recompile.

概要:将 target/scala-XXX 添加为托管源,并删除 File->Project Structure->Modules->Sources 中的应用程序控制器和视图源标志。然后重新编译。

Works on IntelliJ Ultimate 12.1.{4|6}. Created the play application with the command line interface and generated a project file using play idea. Used Play 2.2.0.

适用于 IntelliJ Ultimate 12.1。{4|6}。使用命令行界面创建播放应用程序并使用play idea. 使用 Play 2.2.0。

回答by Aleksandar Stojadinovic

With IntelliJ 14.1 and Play 2.3.8 nothing of the above worked, but the advice from this mailing listworked. (Almost) blatantly copied:

使用 IntelliJ 14.1 和 Play 2.3.8,上述方法均无效,但此邮件列表中的建议有效。(几乎)公然抄袭:

Locate the target/scala-2.11/src_managed and target/scala-2.11/twirl directories in the project view, then right click and Mark Directory As -> Generated Sources (Root).

在项目视图中找到 target/scala-2.11/src_managed 和 target/scala-2.11/twirl 目录,然后右键单击并 Mark Directory As -> Generated Sources (Root)

I bumped the scala version and obviously in newer versions of IntelliJ the Rootword has been added. Also, you cannot select this from the Project Structure window, the option is not available. It is possible only through the Projectpane in the main window. If it refuses to be marked as Generated Sources, try to unmark thetargetdirectory (Mark Directory As -> Unmark ).

我升级了 Scala 版本,显然在较新版本的 IntelliJ 中添加了Root词。此外,您不能从“项目结构”窗口中选择此选项,该选项不可用。只有通过Project主窗口中的窗格才有可能。如果它拒绝标记为Generated Sources,请尝试取消标记target目录(将目录标记为 -> 取消标记)。

回答by Daniel Shin

For people using Play 2.4.x or above, it seems that Play no longer produces reverse routing files for javascript in src_managedet al.

对于使用 Play 2.4.x 或更高版本的人,Play 似乎不再为 javascript 生成反向路由文件src_managed等。

Instead, you need to include scala-2.xx/routes/maindirectory as Sources.

相反,您需要将scala-2.xx/routes/main目录包含为Sources.

回答by Chad

This question was asked a year ago, but to answer for future queries by other coders, the problem is easily solved by adding a "play.Routes" path like this

这个问题是在一年前提出的,但是为了回答其他编码员将来的查询,通过添加这样的“play.Routes”路径很容易解决这个问题

public static Result javascriptRoutes() {
    response().setContentType("text/javascript");
    return ok(
            play.Routes.javascriptRouter("jsRoutes",

                    // Routes for Projects
                    controllers.routes.javascript.Projects.add(),
                    controllers.routes.javascript.Projects.delete(),
                    controllers.routes.javascript.Projects.rename(),
                    controllers.routes.javascript.Projects.addGroup()
            )
    );

}

}

Ensure that you have the proper imports to the class:

确保您对类有正确的导入:

import play.mvc.*;
import play.data.*;

回答by tapsey

I am using Idea 14.1.4 community edition, i managed to remove the index and route not resolved errors by right clicking on the target folder and marking it as not excluded. NB i run my project using the command line i can not find any run configuration in the ide.

我正在使用 Idea 14.1.4 社区版,我设法通过右键单击目标文件夹并将其标记为未排除来删除索引和路由未解决的错误。注意我使用命令行运行我的项目我在ide中找不到任何运行配置。