Незаконное начало простого выражения

Я пытаюсь выполнить простую программу Java с использованием игры. Я получаю ошибку компиляции как недопустимое начало простого выражения. Я искал ответы на этот вопрос, но не получил его. Мой код выглядит так:

@(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>

Я получаю ошибку в цикле for. Можете ли вы помочь мне решить эту проблему?

2 ответа

Фигурные скобки должны быть на одной линии с @for

@for(product <- products){

Обратите внимание на значения в шаблоне scala, если вы используете java в приложении play. Например product.ean будет работать только если вы объявите ean собственность как public в классе Product, Если вы используете классический компонент, вам нужно написать имя метода, например product.getEan

Я сделал проверку вашего кода, и он работает правильно:

модели /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;
  }

}

Контроллеры /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/ маршруты

# Home page
GET     /                           controllers.Application.index()

просмотров /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>

результат:

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

Ваше мнение должно быть:

@(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>
}
Другие вопросы по тегам