Java 播放2模板如何访问列表类型的实体成员
我的实体是:UserShoppingList、产品和类别,如下所示:Java 播放2模板如何访问列表类型的实体成员,java,frameworks,playframework-2.0,Java,Frameworks,Playframework 2.0,我的实体是:UserShoppingList、产品和类别,如下所示: package models; import java.util.*; import play.db.ebean.*; import play.data.validation.Validation; import play.data.validation.Constraints.*; import javax.persistence.*; import com.avaje.ebean.Ebean; /** * En
package models;
import java.util.*;
import play.db.ebean.*;
import play.data.validation.Validation;
import play.data.validation.Constraints.*;
import javax.persistence.*;
import com.avaje.ebean.Ebean;
/**
* Entity managed by Ebean
*/
@Entity
public class UserShoppingList extends Model{
@Id
public Long id;
@Required
public String listName;
@ManyToOne
public User user;
@ManyToMany
public List<Product> products = new ArrayList<Product>();
//getters and setters
//(I have included them, not shown here)
package models;
import java.util.*;
import play.db.ebean.*;
import play.data.validation.Constraints.*;
import javax.persistence.*;
/**
* Product entity managed by Ebean
*/
@Entity
public class Product extends Model{
@Id
public Long Id;
@ManyToOne
public Category category;
@Required
public String brandName;
@Required
public String productName;
// -- Queries
public static Model.Finder<Long,Product> find = new Model.Finder(Long.class, Product.class);
package models;
import java.util.List;
import javax.persistence.*;
import play.db.ebean.*;
import play.data.validation.*;
import play.db.jpa.*;
/**
* Category entity managed by JPA
*/
@Entity
public class Category {
@Id
public Long id;
@Constraints.Required
public String categoryName;
// --getters and setters
封装模型;
导入java.util.*;
导入play.db.ebean.*;
导入play.data.validation.validation;
导入play.data.validation.Constraints.*;
导入javax.persistence.*;
进口com.avaje.ebean.ebean;
/**
*Ebean管理的实体
*/
@实体
公共类UserShoppingList扩展模型{
@身份证
公共长id;
@必需的
公共字符串列表名;
@许多酮
公共用户;
@许多
public List products=new ArrayList();
//接球手和接球手
//(我已将其包括在内,此处未显示)
包装模型;
导入java.util.*;
导入play.db.ebean.*;
导入play.data.validation.Constraints.*;
导入javax.persistence.*;
/**
*由Ebean管理的产品实体
*/
@实体
公共类产品扩展模型{
@身份证
公共长Id;
@许多酮
公共类;
@必需的
公共字符串品牌名称;
@必需的
公共字符串产品名称;
//--询问
publicstaticmodel.Finder=newmodel.Finder(Long.class,Product.class);
包装模型;
导入java.util.List;
导入javax.persistence.*;
导入play.db.ebean.*;
导入play.data.validation.*;
导入play.db.jpa.*;
/**
*JPA管理的类别实体
*/
@实体
公共类类别{
@身份证
公共长id;
@约束条件。必需
公共字符串类别名称;
//--接球手和接球手
我的模板:
@(shoppinglist: List[models.Product], categorylist: List[Category],
userShoppingListsForm: Form[models.UserShoppingList])
@import helper._
@main("Shopping list products") {
var productlist = shoppinglist.getProducts()
<h1>Shopping List</h1>
@for(c <- categorylist){
<ul>
<li class = "categoryrow">@categorylist.getCategoryName()</li>
@for(p <- productlist) {
<ul>
<li class="productcolumn">
@p.getBrandName()
</li>
<li class="productcolumn">
@p.getProductName()
</li>
<li class="command">
id = @p.getId()!
@form(routes.ShoppingLists.deleteProduct(id)) {
<input type="submit" value="Delete">
}
</li>
</ul>
}
</ul>
}
<h2>Add a product to list</h2>
@form(routes.ShoppingLists.newUserShoppingList()) {
@inputText(userShoppingListsForm("listName"))
<input type="submit" value="Create">
}
}
@(shoppinglist:List[models.Product],categorylist:List[Category],
UserShoppingList表单:表单[models.UserShoppingList])
@导入助手_
@主要(“购物清单产品”){
var productlist=shoppinglist.getProducts()
购物清单
@对于(c您希望对list元素使用getter,但您试图在整个list
也许这是正确的解决方案:
<li class = "categoryrow">@c.getCategoryName()</li>
@c.getCategoryName()
要问后续问题,您应该将其作为注释添加到答案下方,并将其编辑到原始问题中。谢谢!非常感谢。我的主要问题是从模板访问列表类型的实体成员。请参阅code@了解(p