-->

play2 framework my template is not seen. : package

2020-08-17 18:14发布

问题:

The problem is that controller doesn't see template I want to use:

[etl_admin] $ compile [info] Compiling 3 Scala sources and 4 Java sources to D:\ECLIPSE_WORKSPACES\play2_apps\etl_admin\target\scala-2.9.1\classes... [error] D:\ECLIPSE_WORKSPACES\play2_apps\etl_admin\app\controllers\EtlWorkflowSeqNodeController.java:7: error: package views.html.etlworkflowseqnode does not exist [error] import views.html.etlworkflowseqnode.list; [error]
^ [error] D:\ECLIPSE_WORKSPACES\play2_apps\etl_admin\app\controllers\EtlWorkflowSeqNodeController.java:14: error: cannot find symbol

[error] list.render(EtlWorkflowSeqNode.findTree(jobId)) [error] ^ [error] symbol: variable list [error]
location: class EtlWorkflowSeqNodeController [error] 2 errors [error] {file:/D:/ECLIPSE_WORKSPACES/play2_apps/etl_admin/}etl_admin/compile:compile: javac returned nonzero exit code [error] Total time: 7 s, completed 05.06.2012 17:14:44

Here is controller code:

package controllers;

import play.mvc.Controller;
import play.mvc.Result;
import models.EtlWorkflowSeqNode;
import play.db.jpa.Transactional;
import views.html.etlworkflowseqnode.list; /*LINE #7, Eclipse really tells that there is no such package*/

public class EtlWorkflowSeqNodeController  extends Controller {

    @Transactional
    public static Result list(Integer jobId) {
        return ok(
            list.render(EtlWorkflowSeqNode.findTree(jobId))
        );
    }
}

I've attached an image with my project tree. There is such package and there is template named "list". what do I do Wrong

回答1:

Omg, the problem was so easy! *classes_managed* (this folder keeps compiled scala templates) were not updated with newly added templates. I did try to call play compile yesterday, it doesn't help. New templates from new package were not compiled. This morning I've called play clean compile aaand... hooray! I did get compiled templates and problem with missing package gone away (don't forget to refresh Eclipse project, force it to update exisitng project structure from file system. It likes to cache everything.)

Sorry for disturbing, seems like I wasn't attentive while reading documentation :(



回答2:

The problem is that Eclipse isn't seeing the src_managed folder, which is dynamically updated by the play framework.

Go to project → properties → java build path → libraries (it's a tab) → add external class folder

Then select the src_managed folder, which should be in the folder target->scala-x.x.x in the same directory as your project.

This will add src_managed to your build path, and Eclipse will now understand that these templates are valid.

You may need to run 'play clean compile' in the play framework console You may then need to run project -> clean in eclipse



回答3:

In Play 2.5, I was able to fix this issue with:

import the view:

import views.html.index;

Then inside the controller:

return ok(index.render("Hello"));

Something like this without complex problems, strange that using it like return ok(views.html.index.render("Hello"))) was not working while importing then use it worked well.

Sure all answers here helpful as well, sometimes the issue simply is to clean and then compile, but even this not really solving issue all the time, regardless the IDE you are using, I was using CLI and was giving cannot find symbol error as well.



回答4:

To resolve "package views.html doesn not exist" :

Run "sbt compile" or "sbt clean compile". That should create a folder structure in your project as target\scala xx\twirl\main\views.html



回答5:

As far as I can tell, you haven't got a package name etlworkflowseqnode! The correct way to import the list template, should be like this:

import views.html.list;

or, if you are going to have multiple views, you can import them all with a wildcard import.

import views.html.*;