0

PlayFramework のチュートリアルを実行しようとしましたが、失敗しました。

index.scala.html:

Compilation error

not found: type Customer

In C:\test\app\views\index.scala.html at line 2.


@main("welcome") {
@(customer: Customer, orders: List[Order]) 

<h1>Welcome @customer.name!</h1>

<ul> 
@for(order <- orders) {
<li>@order.getTitle()</li>

} 
</ul>
}

アプリケーション.java:

public static Result index() {

response().setContentType("text/html");
return ok();

}

答えてください。グーグルが多すぎますが、できません。

4

1 に答える 1

0

モデルをインポートするのを忘れたようです。これを事の前に追加してみてください@main

@import your.package.Customer
@import your.another.package.Order

または、次のようにパッケージ全体をインポートすることもできます。

@import your.package._

編集:はい、この行@(customer: Customer, orders: List[Order])はビューの最初の行であり、@main行の後ではなく、次のようになるはずです。

@(customer: Customer, orders: List[Order]) 

@import your.package._

@main("welcome") {

    <h1>Welcome @customer.name!</h1>

    <ul> 
      @for(order <- orders) {
        <li>@order.getTitle()</li>
      } 
    </ul>
}

Application.javacustomerで、パラメータとordersパラメータをビューに渡す必要があります。

 public static Result index() {

   response().setContentType("text/html");
   return ok(index.render(SomeService.instance().getCustomer(), SomeAnotherService.instance().getOrders()));
 }

EDIT 2:わかりました、これがあなたのやり方です。

Customer クラスと Order クラスは同じパッケージに含まれる場合があり、テンプレート内のインポートは単純な java\scala インポートとまったく同じように機能します。

例:

Customer.java

package models;

public class Customer {
    public String name; //as you use @customer.name thing, the field should be public
}

注文.java

package models;

public class Order {
    private String title; //we can make it private because you use the getter in the template

    public String getTitle() {
         return this.title;
    }

    public void setTitle(String title) {
         this.title = title;
    }
}

アプリケーション.java

 import models.*;
 import static java.utils.Arrays.*;

 ....

 public static Result index() {

    response().setContentType("text/html");
    Customer customer = new Customer();
    customer.name = "serejja";
    Order order1 = new Order();
    order1.setTitle("my order 1");
    Order order2 = new Order();
    order2.setTitle("my order 2");
    java.util.List orders = asList(order1, order2);
    return ok(index.render(customer, orders));
 }

index.scala.html

@(customer: Customer, orders: List[Order]) 

@import models._

@main("welcome") {

    <h1>Welcome @customer.name!</h1>

    <ul> 
      @for(order <- orders) {
        <li>@order.getTitle()</li>
      } 
    </ul>
}
于 2013-09-10T08:11:56.193 に答える