curly braces must be on the same line as @for
@for(product <- products){
Take attention on the values in scala template if you use java in the play app. For example product.ean
would work only if you declare ean
property as public
in the class Product
. If you use classic bean then you need to write method name, like product.getEan
I did verification of your code and it works correctly:
models/Product.java
package models;
public class Product{
private String ean;
private String name;
private String description;
public Product(){};
public String getEan(){
return ean;
}
public void setEan(String ean){
this.ean = ean;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public String getDescription(){
return description;
}
public void setDescription(String description){
this.description = description;
}
}
controllers/Application.java
package controllers;
import play.*;
import play.mvc.*;
import views.html.*;
import models.Product;
import java.util.List;
import java.util.ArrayList;
public class Application extends Controller {
public Result index() {
List<Product> products = new ArrayList<>();
Product product1 = new Product();
product1.setName("p 1");
product1.setEan("ean_1");
product1.setDescription("description 1");
products.add(product1);
return ok(index.render(products));
}
}
conf/routes
# Home page
GET / controllers.Application.index()
views/index.scala.html
@(products :List[Product])
<h1> All Products </h1>
<table class="table table-striped">
<thead>
<tr>
<th> EAN </th>
<th> NAME </th>
<th> DESCRIPTION </th>
</tr>
</thead>
<tbody>
@for(product <- products){
<tr>
<td><a href="@routes.Application.index()"> @product.getEan </a></td>
<td><a href="@routes.Application.index()"> @product.getName </a></td>
<td><a href="@routes.Application.index()"> @product.getDescription </a></td>
</tr>
}
</tbody>
</table>
result:
<h1> All Products </h1>
<table class="table table-striped">
<thead>
<tr>
<th> EAN </th>
<th> NAME </th>
<th> DESCRIPTION </th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="/"> ean_1 </a></td>
<td><a href="/"> p 1 </a></td>
<td><a href="/"> description 1 </a></td>
</tr>
</tbody>
</table>
to the end of file
– JacksonWeekes Sep 25 '15 at 09:45