当我尝试访问应用程序类或模型中的模型时,它会抛出以下错误。我可以创建模型并保留它。但如果我尝试访问它,就会出现问题。
执行异常
[InvalidPropertyException: Invalid property 'id' of bean class [models.CandidateToSalesforce]: No property 'id' found]
In [...]/app/views/syncedRecords.scala.html at line 11.
7 <h3>Results</h3>
8 <ul>
9 @for(r <- records) {
10 <li>
11 @r [[ this is the line]]
12 </li>
13 }
14 </ul>
15}
这可以通过添加 getter 来修复,如下所示。
@Id
public Long id;
// NOTE: play framework was bugging out without this method even though it's supposed to be automatic
public Long getId() {
return id;
}
没什么大不了的,但这不是错误的吗? docs say “Play 被设计为自动生成 getter/setter,以确保与期望它们在运行时可用的库的兼容性”
我是否有一个已知的配置问题会以其他方式困扰我?
最佳答案
您可以在模型中编写自己的toString()
方法:
public String toString(){
return this.id.toString();
}
虽然返回真实的一些字符串而不是 ie 可能是更好的主意。
return this.name+" "+this.somethingElse;
然后,如果您在模板中使用this is my record: @r
,它将呈现 toString()
返回。
您无需编写自定义 getter 即可获取您的 id
:@r.id
(或 Controller 中的 r.id
)
关于playframework-2.0 - id 不会在 PlayFramework 模型中自动可用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10590021/