scala - 如何在 Play 2 模板中访问嵌套模型的属性

标签 scala attributes playframework nested playframework-2.0

我在 Play 2 项目中拥有这两个模型:

@Entity
public class Site extends Model{

    @Id
    public String adId;

    @Required
    public String name;

    @Required
    @OneToMany (cascade = CascadeType.ALL)
    public List<Room> rooms;
...
}//end of class

@Entity
public class Room extends Model {

    @Id
    @Required
    public String adId;

    @Required 
    public String name;

    public List<Reservation> reservations = new ArrayList<Reservation>();

...
}

当我在 view.scala.html 中使用此代码时

            var treeData = [                            
                    @for(site <- sites) {
                        {title: "@site.name", tooltip: "@site.adId",
                            children: [
                                @for(room <- site.rooms) {
                                    {title: "@room.name", tooltip: "@room.adId"},
                                }
                            ]
                        },
                    }
                ];

我无法访问site.rooms 如果我在服务器端测试它,数据是存在的。

另一个奇怪的事情是我确实得到了 site.adId,但 site.name 保持为空...

如果有人知道我做错了什么......

(抱歉英语不好,这不是我的母语)。

最佳答案

您需要 getter 才能在模板/scala 中访问数据。至少这为我解决了这个问题。我想我在 mailinglist 上找到了该信息:

This issue has been discussed before. You must use private field and public accessor methods if you want to use Ebean lazy loading (or any other Java magic) from Scala code.

关于scala - 如何在 Play 2 模板中访问嵌套模型的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10893602/

相关文章:

arrays - 在scala中打印类似表格的二维数组

python - Href 在 scrapy 结果中不可见,但在 html 中可见

数组上的 ASP.NET MVC Range 属性

python - 如果找不到属性,如何使 XPath 在 Python 中返回 'None'?

scala - 编译Play Framework项目时出现"IO error while decoding Routes.scala with UTF-8"

mysql - 连接到 MySql 的 Scala Play 显示运行时错误 "Table ' token '不存在”

scala - 从参数化类型引用抽象类型

scala - Logback 依赖性阻止 SBT 离线运行

java - 使用 play/Ebean 将数据保存到数据库中的问题

java - Intellij 和 sbt 0.13.5 "URI has an authority component"