2

I am trying to execute a simple java program using play. I am getting a compilation error as illegal start of simple expression. I have searched answers for this question, but I didn't get it. My code looks like this:

@(products :List[Product])
@main("Products catalogue")
<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.Products.details(product.ean)"> @product.ean </a></td>
<td><a href="@routes.Products.details(product.ean)"> @product.name </a></td>
<td><a href="@routes.Products.details(product.ean)"> @product.description  </a></td>
</tr>
}
</tbody>
</table>

I am getting the error at the for loop. Can u help me to solve this problem?

Daniel Olszewski
  • 12,152
  • 4
  • 52
  • 58
  • Please add the tag for `play framwork`. So, respective person may reach up to your question. – Kruti Patel Sep 25 '15 at 08:58
  • You need to surround your html with curly braces- from just before

    to the end of file

    – JacksonWeekes Sep 25 '15 at 09:45
  • thank u. But i am getting error at main like: missing arguments for method apply in object main; follow this method with `_' if you want to treat it as a partially applied function .... after surrounding html with curly braces. what's the solution? – Rekha Ghasni Sep 25 '15 at 09:55
  • Please add screen shots of the errors you are getting so I can assist you further – JacksonWeekes Sep 25 '15 at 10:35

2 Answers2

1

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>
Andriy Kuba
  • 7,595
  • 2
  • 22
  • 42
0

Your view needs to be:

@(products :List[Product])
@main("Products catalogue") {
<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.Products.details(product.ean)"> @product.ean </a></td>
<td><a href="@routes.Products.details(product.ean)"> @product.name </a></td>
<td><a href="@routes.Products.details(product.ean)"> @product.description  </a></td>
</tr>
}
</tbody>
</table>
}
Gus
  • 4,058
  • 2
  • 22
  • 27