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

流过昼夜 提交于 2019-11-29 14:19:14

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.

It looks like Task.all() returns a Java list, while the scala template is probably expecting a Scala list.

I woul suggest changing the return type of Task.all() if possible, or fully qualified the definition in the template:

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

Although biesior's answer has some nice insights and tips, it did not solve the issue.
At the end I have abndoned type TypeSafe Activator and created the site from scratch using play comamnd line and it worked perfectly.

I never found the origin of the issue in the question, I am leaving this answer for a future reference for the googlers.

If any one has a better solution, please leave your answer and if it works I will mark it as accepted.


EDIT:

@biesior was kind enough to go through my code and he did find the issue. If you have the same issue, take a look at his answer.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!