【发布时间】:2017-09-21 14:44:57
【问题描述】:
我已经尝试了所有可能的方法,但我得到的解决方案我自己并不喜欢。
我正在使用 Spring Framework 和 Thymeleaf。在我的实体类中,我将我的属性声明为私有,如下所示
public class Subscriber {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
@Column(name= "firstname")
private String firstname;
@Column(name= "lastname")
private String lastname;
@Column(name= "email")
private String email;
public Subscriber(){
}
}
在 Thymeleaf 中,我正在使用 th: tyo 从我的数据库中获取数据,如下所示:
<tr th:each="subscriber : ${subscribers}">
<td th:text="${subscriber.firstname} + ' ' + ${subscriber.lastname}"></td>
<td th:text="${subscriber.email}"></td>
当我运行代码时,我在运行时收到以下错误:
org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'firstname' cannot be found on object of type '' - maybe not public?
现在,如果我将修饰符更改为 public,一切正常,我的数据就会显示出来。但是,我不认为这是为实体建模的最佳方式。我需要警惕将来可能访问我的代码库的第三方,从而防止他们修改我的代码并对我造成损害。
因此,我需要任何更有经验的人帮助我了解如何绕过此问题,而无需将修饰符从私有更改为公共。
感谢任何帮助。
【问题讨论】:
-
我不确定,但请尝试将 getter 添加到您的实体。
-
我在实体中已经有了getter和setter。
标签: spring-mvc spring-boot spring-data-jpa thymeleaf