モデルをインポートするのを忘れたようです。これを事の前に追加してみてください@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>
}