id不能自动在Play Framework模型中使用?

时间:2012-05-14 19:44:31

标签: playframework-2.0

当我尝试访问Application类或模型中的模型时,它会抛出以下错误。我可以创建模型并坚持下去。但是,如果我尝试访问它,问题。

执行例外

[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,以确保与期望它们在运行时可用的库兼容”

我是否有一个已知的配置问题会以其他方式咬我?

1 个答案:

答案 0 :(得分:2)

您可以在模型中编写自己的toString()方法:

public String toString(){
     return this.id.toString();
}

尽管可能更好的想法是返回真正的字符串而不是ie。

return this.name+" "+this.somethingElse;

然后,如果您在模板中使用this is my record: @r,则会呈现toString()返回。

您可以使用id(或控制器中的@r.id)来提取r.id w / out编写自定义getter