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

标签 java scala playframework

我在 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/

相关文章:

scala - 在这些情况下,Scala 值类将为 "boxed",对吗?

java - 后台缓存逐出进程无法释放 [10]% 的缓存用于上下文

java - 如何使用java读取多个JSON文件并将其写入单个JSON文件

scala - 全局禁用 sbt supershell

scala - 不能将 futures-util crate 与 Actix 一起使用,因为特征 Future 未实现

html - 在 Play 2.3.x 中生成数据库表的 <table>

java - 使用 Play 框架将生成的图像发送到浏览器

scala - 如何使用 Play Silhouette 实现无状态 cookie 身份验证?

java - 如何将 onClick 用于多个 ImageView 以启动其他 Activity

java - 单击菜单栏项目后尝试启动新的 Tic Tac Toe 游戏