Play Framework 2.2.1 - 编译错误:“类索引中的方法渲染不能应用于给定类型;" [英] Play Framework 2.2.1 - Compilation error: "method render in class index cannot be applied to given types;"

查看:26
本文介绍了Play Framework 2.2.1 - 编译错误:“类索引中的方法渲染不能应用于给定类型;"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Play Framework 的新手,并试图从 this 构建一个待办事项列表 手册.

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 一个,但我无法解决使用它的问题(可能是由于对环境/框架缺乏了解......).

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 不是 Models 不是吗?

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

尤其是这些行:

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

TBH 我从未测试过 Activator,但看起来将 play.api.data.Form 导入视图,这对于 Java 控制器来说是不正确的.解决方案是表单的完全限定路径:

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.

这篇关于Play Framework 2.2.1 - 编译错误:“类索引中的方法渲染不能应用于给定类型;"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆