我有一个 pojo 用户类,它有私有(private)字段和公共(public) getter/setter。当我尝试使用模板表达式语言访问私有(private)字段时,播放框架会生成编译时错误。以下是我的代码:
型号类:
public class User extends AbstractEntity{
@Id
@Getter @Setter
private Long id;
@Required
@Getter @Setter
private String name;
@Getter @Setter
private String age;
@Getter @Setter
private List<Car> cars;
public User() {}
public User(Long id, String name, String age){
this.id = id;
this.name = name;
this.age = age;
}
}
在模型类中,我正在使用项目
lombok
用于生成 getter/setter
.模板 :
<h1>All Users</h1>
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
</tr>
</thead>
<tbody>
@for(user <- users){
<tr>
<td>@user.id</td>
<td>@user.name</td>
<td>@user.age</td>
</tr>
}
</tbody>
</table>
编译代码时,播放产生以下错误:
[error] /home/james/eclipse_workspace/playcrud/play-crud/app/views/user/all_users.scala.html:16:
variable id in class User cannot be accessed in com.harmeetsingh13.entities.User
[error] <td>@user.id</td>
最佳答案
Play 的“模板表达式语言”就是简单的 Scala 代码。由于您要访问的字段是私有(private)的,因此您应该使用公共(public) getter。
@for(user <- users){
<tr>
<td>@user.getId</td>
<td>@user.getName</td>
<td>@user.getAge</td>
</tr>
}
关于compiler-errors - 播放框架 2.2 : How to access model private field in temlate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27684774/