eclipse 在 scala ide 中打开 scala play 项目时“对象索引不是包 views.html 的成员”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28458597/
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
"object index is not a member of package views.html" when opening scala play project in scala ide
提问by user384842
I've created a play project with play 2.3.7
我用 play 2.3.7 创建了一个 play 项目
In the project directory, I ran activator and ran the eclipse
command to generate eclipse project files.
在项目目录下运行activator,运行eclipse
命令生成eclipse项目文件。
When I go to eclipse (I'm using the Scala IDE from typesafe Build id: 4.0.0-vfinal-20150119-1023-Typesafe , there is an error in my Application.scala file:
当我去 Eclipse 时(我使用的是来自 typesafe Build id: 4.0.0-vfinal-20150119-1023-Typesafe 的 Scala IDE,我的 Application.scala 文件中有一个错误:
object index is not a member of package views.html
Is there something amiss with my setup? The app runs fine when I execute run
at the activation console prompt.
我的设置有问题吗?当我run
在激活控制台提示下执行时,该应用程序运行良好。
Thanks!
谢谢!
EDIT: Added code
编辑:添加代码
package controllers
import play.api._
import play.api.mvc._
object Application extends Controller {
def index = Action {
Ok(views.html.index("Your new application is ready."))
}
}
The error is on the 'Ok..' line.
错误在“确定...”行上。
There is a file in views called index.scala.html, and the app runs file when I run it from activator..
视图中有一个名为 index.scala.html 的文件,当我从激活器运行它时,该应用程序会运行该文件。
回答by Brent Faust
Occasionally after adding a view in Play 2.4.x, IntelliJ IDEA sometimes gets confused and absolutely refuses to build. Even rebuild Project fails:
偶尔在 Play 2.4.x 中添加视图后,IntelliJ IDEA 有时会感到困惑并且绝对拒绝构建。即使重建项目失败:
This still happens from time-to-time in IDEA 15. And when it does, the command line provides the quickest, most-reliable fix:
这种情况在 IDEA 15 中仍然不时发生。当它发生时,命令行提供了最快、最可靠的修复:
sbt clean; sbt compile
sbt 干净;sbt 编译
That's it! IDEA will now compile the project as expected.
就是这样!IDEA 现在将按预期编译项目。
Update:
更新:
In the rare case that sbt compile
completed successfully on the command line, but IntelliJ IDEA 15 still gives the same "object x is not a member" error, then this has solved IDEA's confusion:
在极少数情况下,sbt compile
在命令行上成功完成,但 IntelliJ IDEA 15 仍然给出相同的“对象 x 不是成员”错误,那么这就解决了 IDEA 的困惑:
File Menu:
文件菜单:
回答by einnocent
The other solutions did not work for me. Some would give me different errors, some would clear the Problems tab but leave me with a red squiggle under views.html.index
and auto-complete would not work with the scala.html templates.
其他解决方案对我不起作用。有些会给我不同的错误,有些会清除“问题”选项卡,但在下面留下红色波浪线,views.html.index
并且自动完成功能不适用于 scala.html 模板。
What finally worked was to open the project's properties, go to Java Build Path > Source
, and add both of the following directories:
最终成功的是打开项目的属性,转到Java Build Path > Source
,然后添加以下两个目录:
target/scala-2.11/src_managed/main
target/scala-2.11/twirl/main
If you only do target/scala-2.11/twirl/main
then you'll miss out on the class files generated from the conf
directory.
如果您只这样做,target/scala-2.11/twirl/main
那么您将错过从conf
目录生成的类文件。
回答by Luis Rodero-Merino
In Scala IDE 4.0.0 thinks there's errors in an out-of-the-box Play Framework 2.3.7 programyou can find the solution (in brief: adding target/scala-2.11/twirl/main
folder to the compilation path), give it a try.
在Scala IDE 4.0.0 中认为开箱即用的 Play Framework 2.3.7 程序中存在错误,您可以找到解决方案(简而言之:将target/scala-2.11/twirl/main
文件夹添加到编译路径),试一试。
回答by Chip France
I had the same problem. I added target/scala-2.x/classes
and target/scala-2.x/classes_managed
to my Java build path and Eclipse stopped complaining.
我有同样的问题。我将target/scala-2.x/classes
和添加target/scala-2.x/classes_managed
到我的 Java 构建路径,Eclipse 停止抱怨。
回答by Lera
I had the same issue running Play 2.4.0-RC1 using default SBT layout (disablePlugins(PlayLayoutPlugin)
) and solved it by adding to build.sbt
:
我在使用默认 SBT 布局 ( disablePlugins(PlayLayoutPlugin)
)运行 Play 2.4.0-RC1 时遇到了同样的问题,并通过添加到build.sbt
:
sourceDirectories in (Compile, TwirlKeys.compileTemplates) :=
(unmanagedSourceDirectories in Compile).value
回答by Surya Prabhakar
Adding target/scala-2.11/twirl/main
which is having views.html
package to source fixed for me.
添加target/scala-2.11/twirl/main
哪个views.html
包为我修复了源代码。
回答by David Hinson
@brent-foust 's answer worked for me but only initially. Every time I rebuilt the project from within IDEA I would then get "not found: routes" errors from within target\scala-2.11\twirl\main\views\html\main.template.scala
until I performed Brent's workaround again.
@brent-foust 的回答对我有用,但只是最初。每次我从 IDEA 中重建项目时,我都会从内部收到“未找到:路由”错误,target\scala-2.11\twirl\main\views\html\main.template.scala
直到我再次执行 Brent 的解决方法。
I eventually discovered the solution to that was changing a line in the .iml file from
我最终发现解决方案是从 .iml 文件中更改一行
<excludeFolder url="file://$MODULE_DIR$/target/scala-2.11/src_managed/main" />
to
到
<sourceFolder url="file://$MODULE_DIR$/target/scala-2.11/src_managed/main" isTestSource="false" />
I don't know what the long term implications of doing this are but it has fixed this problem. Some of the other similar problems mentioned might also be fixed by applying the same change to some of the other folders listed in the .iml.
我不知道这样做的长期影响是什么,但它已经解决了这个问题。提到的一些其他类似问题也可以通过对 .iml 中列出的其他一些文件夹应用相同的更改来修复。
回答by Nabil A.
I tried all solutions without any positiv result.
So I went to Preferences > Build, Execution, Deployment > Build Tools > sbt
and checked Use sbt shell
for imports and builds.
This let the compile button in intelliJ compile with the sbt shell.
I think this is better anyway, since a build server or something similar will compile the same way and not like intelliJ.
我尝试了所有解决方案,但没有任何积极的结果。
所以我去Preferences > Build, Execution, Deployment > Build Tools > sbt
检查Use sbt shell
进口和构建。
这让 intelliJ 中的编译按钮使用 sbt shell 进行编译。我认为无论如何这更好,因为构建服务器或类似的东西将以相同的方式编译,而不是像 intelliJ。
回答by user2038596
1) Add the following line to your sbt.build file:
1) 将以下行添加到您的 sbt.build 文件中:
EclipseKeys.preTasks := Seq(compile in Compile)
2) Add the follwing line to your plugins.sbt file under the project folder:
2) 将以下行添加到项目文件夹下的 plugins.sbt 文件中:
addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "5.1.0")
3) Run the "eclipse" command from within sbt
3) 在 sbt 中运行“eclipse”命令
as explained in the documentation of the play framework:
如播放框架的文档中所述:
回答by Som Bhattacharyya
Basically we need a way to put the compiled classes on the path for this to work.
基本上我们需要一种方法来将编译的类放在路径上以使其工作。
I did the following to fix it.
Since the projects compiles to the target
directory.
I went to the Project Properties
-> Java Build Path and added a few folders that look like this,
我做了以下修复它。由于项目编译到target
目录。我去了Project Properties
-> Java Build Path 并添加了一些看起来像这样的文件夹,
target/scala-2.12/routes/main target/scala-2.12/twirl target/scala-2.12/twirl/main
target/scala-2.12/routes/main target/scala-2.12/twirl target/scala-2.12/twirl/main
Now i dont want you to assume you will have these exact folders in your case too. That depends on your project setup. But you should add the folders inside the target/scala-2.x folder.
现在我不希望您假设您的案例中也会有这些确切的文件夹。这取决于您的项目设置。但是您应该将文件夹添加到 target/scala-2.x 文件夹中。