how to render arraylist or list and to handle it in template play framework 2.x

孤者浪人 提交于 2019-12-23 02:59:10

问题


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

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.

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.

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


回答1:


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).

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>
}


来源:https://stackoverflow.com/questions/14715962/how-to-render-arraylist-or-list-and-to-handle-it-in-template-play-framework-2-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!