java Play framework 2.2.0不会自动生成getter/setter
在处理游戏框架时,我发现了一个非常恼人的情况,我不得不花很多时间指出邪恶的根源在哪里
为了简化情况,让我们考虑下面的代码:在controllers.Application.java
中:
return ok(views.html.method1.render(Person.find.all());
在method1.scala.html
中:
@(people : List[Person])
...
@for(person <- people) {
@person.name
@person.pet.getName()
@person.pet.name
}
在Person.java
中:
@Id
public Long id;
@ManyToOne
public Pet name;
...
问题是当我在Pet
类中手动设置getName()方法时。返回该值。
但当我让Play框架自动生成时,它不会返回任何值
当然,Play框架已经为person
生成了getter,我可以通过person.name
访问它
它不应该自动生成吗
# 1 楼答案
http://www.playframework.com/documentation/2.2.x/JavaEbean表示getter和setter是在运行时生成的(对于需要它们的普通旧Java库),在编译时不可见:
因此,getter在模板中不可见。 如果您需要延迟加载(请参见2)),我建议您让您的getter和setter由IDE生成。 如果您不需要延迟加载,只需访问字段,它们仍然是
public
顺便说一句:在类
Person
中引用名为name
的类Pet
听起来像是一个笨拙的数据模型,无意冒犯