Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
703 views
in Technique[技术] by (71.8m points)

java - Play Framework 2.2.1 - Compilation error: "method render in class index cannot be applied to given types;"

I am new to Play Framework, and attempting to build a Todo-list from this manual.

When I try to run the application I get the error:

Compilation Error
error: method render in class index cannot be applied to given types;

My code is (the relevant parts):

MainController.java:

final static Form<Task> taskForm = Form.form(Task.class);

public static Result tasks() {
    return ok(
            views.html.index.render(Task.all(), taskForm)
    );
}

index.sacala.html:

@(tasks: List[Models.Task], taskForm: Form[Models.Task])

I looked around, the closest thread I found was this one, but I was not able to resolve the issue using it (might be due to lack of understanding of the environment / framework...).

One last thing that is worth mentioning:
If I change 'index.sacala.html' to be parameter-less (and change the 'MainController' accordingly, everything works perfectly.

Would appreciate any thoughts about resolving this compilation error.

EDIT:
The Task.all() code is:

public static List<Task> all() {
    return new ArrayList<Task>();
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Most probably your package is models NOT Models isn't it?

BTW this package is autoimported so you can just use:

 @(tasks: List[Task], taskForm: Form[Task])

Hm, changes... Actually log in the console says everything

[error] /www/play20apps/testing/Todo-List/app/controllers/MainController.java:24: error: method render in class index cannot be applied to given types;
[error]         return ok(views.html.index.render(Task.all(), taskForm));
[error]                                   ^
[error]   required: List<Task>,play.api.data.Form<Task>
[error]   found: List<Task>,play.data.Form<Task>
[error]   reason: actual argument play.data.Form<Task> cannot be converted to play.api.data.Form<Task> by method invocation conversion
[error] 1 error

especially these lines:

[error]   required: List<Task>,play.api.data.Form<Task>
[error]   found: List<Task>,play.data.Form<Task>

TBH I didn't ever test the Activator but it looks that imports play.api.data.Form into views, which is incorrect for Java controllers. solution is full qualified path for Form:

@(tasks: java.util.List[Task], taskForm: play.data.Form[Task])

As mentioned in comment *.api.* imports are for Scala and normal are for Java, that's the rule of the thumb in Play 2.+

PostScriptum: Just realized that in your build.sbt you have play.Project.playScalaSettings and actually it should be play.Project.playJavaSettings, this change fixes your problems with Activator.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...