gpt4 book ai didi

java - 玩2模板如何访问列表类型的实体成员

转载 作者:行者123 更新时间:2023-12-01 04:56:12 25 4
gpt4 key购买 nike

我的实体是: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;

/**
* 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 框架创建模板来访问列表本身的实体成员(例如:“UserShoppingList”中的产品吗?

非常感谢任何帮助。谢谢一百万

最佳答案

您想要对列表元素使用 getter,但您试图在整个 List 上使用它

这可能是正确的解决方案:

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

关于java - 玩2模板如何访问列表类型的实体成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14094827/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com