gpt4 book ai didi

java - 将 List 对象从 Controller 传递到 View 时 Play Framework 编译错误

转载 作者:行者123 更新时间:2023-12-02 03:15:15 31 4
gpt4 key购买 nike

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

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

该函数工作正常并返回一个 List 对象。

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

@(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>

最佳答案

更改此行

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 和用户传递给表单,这是错误的。

关于java - 将 List 对象从 Controller 传递到 View 时 Play Framework 编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40410135/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com