如何在模板播放框架2.x中呈现arraylist或list并进行处理 [英] how to render arraylist or list and to handle it in template play framework 2.x

查看:66
本文介绍了如何在模板播放框架2.x中呈现arraylist或list并进行处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我熟悉Play 1.x.但是我必须使用cassandra DB.这就是为什么我不得不使用Play 2.x

I was familiar with Play 1.x. But I had to use cassandra DB. That is why I had to use Play 2.x

但是Scala确实混合了所有内容.模板在Play 2.x中是非常不同的,例如,在Play 1.x中,我可以发送任何参数,例如Stringintobject实例,arraylist等.

However Scala mixing everything really. Template is very different in Play 2.x For example, in Play 1.x I just could send any parameter as String, int, object instance, arraylist etc.

在渲染到视图模板中,并且可以轻松地在模板中使用这些参数.我不知道如何在Play 2.x中成功实现此功能,只有一个渲染器,它仅使我能够渲染String.

In render to the view template and could use these paramaters easly in template. I dont know how to succed this in Play 2.x there is only a render and it just enable me to render a String.

我在Play 2.x文档中阅读了有关此内容的内容.您能给我看看一些例子还是给我一些有关的教程吗?

I read something about this in Play 2.x document. Could you please show me some examples or point me some tutorials about that?

推荐答案

Play 2.x中,每个视图都被编译为Scala函数,因此出于securityperformancecompilation的原因意味着您必须在视图中声明参数(及其类型),幸运的是,它可以是您希望的任何类型(不仅是String).

In Play 2.x every view is compiled to the Scala function, so for security, performance and compilation reasons means that you must to declare arguments (and their types) in the views and fortunately it can be any type you wish (not only String).

例如,如果您有模型model.Book并想将model.BookList传递给视图,则需要在view的第一行中声明它:

For an example if you have model model.Book and want to pass a List of model.Book to the view, you need to declare it in the first line of the view:

/app/controllers/Application.java:

public static Result listAllBooks(){
    List<Book> books = Book.find.all();
    return ok(listAllBooksView.render(books));
}

/app/views/listAllBooksView.scala.html

@(books: List[Book])

@for(book <- books){
    @book.title <br>
}

这篇关于如何在模板播放框架2.x中呈现arraylist或list并进行处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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