Play Framework 2.2.1 - 编译错误:“类索引中的方法呈现不能应用于给定类型;”

Play Framework 2.2.1  - 编译错误:“类索引中的方法呈现不能应用于给定类型;”

问题描述:

我是Play Framework的新手,并试图从这个建立Todo列表手册。

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])

我环顾四周,找到的最接近的线程是 this one,但我无法使用它来解决问题(可能是由于对环境/框架缺乏了解......)。

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...).

值得一提的最后一件事:

如果我将'index.sacala.html'更改为无参数(并更改''因此,MainController'一切都运行良好。

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.

编辑:

Task.all()代码为:

public static List<Task> all() {
    return new ArrayList<Task>();
}


很可能你的包是 models NOT Models 不是吗?

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

BTW这个包是自动导入的,所以你可以使用:

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

特别是这些行:

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

TBH我没有测试过激活器,但看起来导入播放.api.data.Form 进入视图,这对Java控制器是不正确的。解决方案是Form的完全限定路径:

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])

如评论中所述 * .api。* 导入用于Scala,而普通用于Java,这是Play 2中的拇指规则。+

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:刚才意识到你的 build.sbt 你有 play.Project.playScalaSettings ,实际上它应该是 play.Project.playJavaSettings ,此更改修复了Activator的问题。

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.