问题
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