2012-12-30 40 views
1

我的實體是:UserShoppingList,產品和類別如下:播放2模板如何訪問類型列表中的實體成員

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 

我的模板:

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

     } 



} 

我收到以下錯誤:

value getCategoryName is not a member of java.util.List[models.Category]

有人可以建議我更好的方法來創建模板爲基於Java的Play 2框架來訪問權限y列表本身是列表(例如:請在「UserShoppingList」中的產品?

任何幫助極大的讚賞。萬分感謝

+0

要問的後續問題,你應該在答案下面添加它作爲評論,並將其編輯爲原始問題。謝謝! – irrelephant

回答

0

您要使用列表元素一個getter,但是你想使用它的整個List

也許這是正確的解決方案:

<li class = "categoryrow">@c.getCategoryName()</li> 
+0

非常感謝。我的主要問題是訪問模板列表類型的實體成員。請參閱代碼@for(p < - productlist){給出錯誤:未找到:值productlist。我在模板中將其定義爲「\t var productlist = shoppinglist.getProducts()」。 – user1938501

相關問題