非法开始简单表达

时间:2015-09-25 08:48:13

标签: for-loop playframework twirl

我正在尝试使用play执行一个简单的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 个答案:

答案 0 :(得分:1)

花括号必须与@for

在同一行
@for(product <- products){

如果在Play应用程序中使用java,请注意scala模板中的值。例如,product.ean只有在类ean中将public属性声明为Product时才有效。如果使用经典bean,则需要编写方法名称,如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>

答案 1 :(得分:0)

您的观点必须是:

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