Newly created scala.html views are not being recognized in Play Framework 2.x
Asked Answered
S

7

15

I am using Play Framework 2.0.1. I have created a Java application using the "play new" command. By default, two views are created: index.scala.html and main.scala.html

I've done a few sample tutorial apps that allow me to render those views. However, when I add a new view (by default in app/views/), I get a compilation error that it cannot be found:

public static Result getAllCars() {
    List<Car> cars = Car.getAllCars();
    return ok(simpleCarView.render(cars));
}

I can do

import views.html.index;
import views.html.main;

but not

import views.html.simpleCarView; 

Error in console:

cannot find symbol
[error] symbol : variable simpleCarView
[error] location: class controllers.Application

I've tried adding scala.html views in the file directory and from within eclipse, but for some reason they are not found. I've also tried restarting the default Netty server.

Any ideas on what is causing this?

Slogan answered 16/5, 2012 at 20:42 Comment(1)
have the same issue with playframework 2.1.3. The problem was that IntelliJ idea optimized my imports like this: import views.html.index$;Hilaria
L
25

The views are not compiled by Eclipse but can be seen by eclipse after they're compiled by Play as long as the target\scala-2.9.1\classes_managed directory is in your eclipse project build path.

Try running "play compile" on the command line (or just "compile" if you're already in the play console) and then refreshing your project within eclipse (select the project and hit F5)

Lully answered 17/5, 2012 at 1:7 Comment(5)
I have done "play compile" and I get the error I posted in my question. When I compile, it says two scala sources and 1 Java source is being compiled. I'm assuming it's talking about my Application.java and index.scala.html and main.scala.html. But other views are not compiling under the views folder.Slogan
After doing a lot of refreshing, deleting/readding views, re-compile, etc... the problem seems to have gone away. I've been unable to reproduce the odd behavior I described earlier. In general your answer describes the best steps to go through, so I'll mark it as the answer.Slogan
@Matt the key is to comment out the line in your Controller that is giving the compiler error (red underline) before running play compile or you will keep seeing that error. Once you've commented that out and run play compile, you can refresh eclipse, uncomment the line, and import the view to the controller. Hopefully that clarification saves some headaches for people who come across this.Catamnesis
This really sucks. My feeling is we don't really need a framework unless it really acts like one...Plutus
This doesn't work anymore because the there is no scala-2.4.2 folder in the target folder anymoreMorrill
T
4

For you IntelliJ 12 users out there: I upgraded to Play 2.1 which broke my Play IntelliJ Support plugin. This caused IntelliJ to not recognise:

import views.html.*;

so when hitting cmd + o to optimize my imports it was removed. This resultet in a compilation error when running play clean compile since the views were not imported:

[error] symbol  : variable index
[error] location: class controllers.Application
[error]         return ok(index.render());
[error]                   ^
[error] 1 error
[error] (compile:compile) javac returned nonzero exit code
[error] application -

So I uninstalled the plugin, restarted IntelliJ and viola everything works like a charm!

Trojan answered 20/2, 2013 at 9:10 Comment(0)
S
3

Your can use

~compile

in play console so that updated templates will get recompiled on file change and probably Eclipse will see changes immediately (IDEA does that).

Skippy answered 17/5, 2012 at 9:40 Comment(1)
Works well. You can also use ~run to get the templates automatically recompiled on the fly while running the server.Disprize
D
2

This happened to me after I copied an entire project and tried to modify it. The changes in the HTML views would just be ignored since they were not compiled.

Doing activator clean compile run fixed the problem.

Dilapidated answered 12/11, 2015 at 20:18 Comment(0)
H
1

I have had this behaviour as well. Turns out it was a typical copy/paste problem. I forget to update the import statement.

Heirdom answered 26/5, 2012 at 11:7 Comment(0)
C
0

I do the same play compile that InPursuit suggested, but when adding a completely new view I've found that I need to close the project and reopen it so that eclipse rebuilds its "content assist" information.

I just right-click the project and choose "Close Project", and then right-click the now empty project folder and choose "Open Project". It's annoying but that's the only way (short of restarting eclipse) that has worked for me. Sometimes the red squigglies are still there after reopening but they go away if I do a refresh.

Charinile answered 18/5, 2012 at 19:15 Comment(0)
N
0

In my case I just added the following import:

import views.html.*;

The Eclipse IDE removes the mentioned line if you organize your imports through CTRL + SHIFT + O

Neri answered 25/10, 2017 at 17:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.