将List对象从控制器传递到视图时播放框架编译错误

时间:2016-11-03 19:54:10

标签: java scala playframework

我在控制器中有一个函数,它返回用户模型记录列表 - 我正在使用PlayStartApp模板:

public Result getAllUsers() {
    List<User> users = User.find.all();
    return ok(searchusers.render(form(Login.class, users)));
}

此函数正常工作并返回List对象。

我还有一个用它设置的视图(html页面)将对象传递给视图:

@(loginForm: Form[Application.Login], userList: java.util.List[User])

当我在命令行上编译激活器时,收到此错误消息:

[PlayStartApp] $ compile
[info] Compiling 1 Scala source and 1 Java source to C:\WebDev\git\PlayAuthentic
ate\target\scala-2.10\classes...
[error] C:\WebDev\git\PlayAuthenticate\app\controllers\Application.java:209: met
hod render in class views.html.searchusers cannot be applied to given types;
[error]   required: play.data.Form<controllers.Application.Login>,java.util.List
<models.User>
[error]   found: play.data.Form<controllers.Application.Login>
[error]   reason: actual and formal argument lists differ in length
[error] searchusers.render
[error] C:\WebDev\git\PlayAuthenticate\app\controllers\Application.java:215: no
suitable method found for form(java.lang.Class<controllers.Application.Login>,ja
va.util.List<models.User>)
[error]     method play.data.Form.<T>form(java.lang.Class<T>,java.lang.Class<?>)
 is not applicable
[error]       (cannot infer type-variable(s) T
[error]         (argument mismatch; java.util.List<models.User> cannot be conver
ted to java.lang.Class<?>))
[error]     method play.data.Form.<T>form(java.lang.String,java.lang.Class<T>,ja
va.lang.Class<?>) is not applicable
[error]       (cannot infer type-variable(s) T
[error]         (actual and formal argument lists differ in length))
[error]     method play.data.Form.<T>form(java.lang.String,java.lang.Class<T>) i
s not applicable
[error]       (cannot infer type-variable(s) T
[error]         (argument mismatch; java.lang.Class<controllers.Application.Logi
n> cannot be converted to java.lang.String))
[error]     method play.data.Form.<T>form(java.lang.Class<T>) is not applicable
[error]       (cannot infer type-variable(s) T
[error]         (actual and formal argument lists differ in length))
[error] form
[info] Some messages have been simplified; recompile with -Xdiags:verbose to get
 full output
[error] (compile:compileIncremental) javac returned nonzero exit code

我查看过这篇文章,但我仍然遇到同样的问题: Play Framework 2.2.1 - Compilation error: "method render in class index cannot be applied to given types;"

任何帮助都会很棒!

编辑: 我删除了登录表单。代码现在是:

public Result getAllUsers() {
    List<User> users = User.find.all();
    return ok(searchusers.render(users));
}

我的观点现在有:

@(userList: List[User])


@main(null) {

<ul>
@for(user <- userList) {
  <li>@user.fullname</li>
} 
</ul>

}

我在编译时收到了这个:

[error] C:\WebDev\git\PlayAuthenticate\app\controllers\Application.java:209:  no
 instance(s) of type variable(s) T exist so that play.data.Form<T> conforms to java.util.List<models.User>

1 个答案:

答案 0 :(得分:2)

更改此行

ok(searchusers.render(form(Login.class, users)))

ok(searchusers.render(form(Login.class), users))

为了清楚起见

Form[Application.Login] loginForm = form(Login.class)

ok(searchusers.render(loginForm, users))

你必须将表单作为第一个参数传递,将用户作为第二个参数传递,但是你试图将Login.class和用户传递给错误的表单。