Reputation: 7366
I have tried everything possible but the solution I get I do not like it myself.
I am using Spring Framework and Thymeleaf. In my entity class, I declared my attribute as private as below
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(){
}
}
And in Thymeleaf, I am using th: tyo fetch data from my database as below:
<tr th:each="subscriber : ${subscribers}">
<td th:text="${subscriber.firstname} + ' ' + ${subscriber.lastname}"></td>
<td th:text="${subscriber.email}"></td>
when I run the code, I'm getting the following error at runtime:
org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'firstname' cannot be found on object of type '' - maybe not public?
Now if I change the modifier to public everything works fine and my data gets displayed. However, I do not believe this is the best way to model the entity. I need to be wary of the third party that may have access to my code base in the future and thus prevent them from modifying my code and cause damage to me.
I therefore need help from anyone more experienced on how this can be bypassed without the need to change the modifier to public from private.
Any assistance is appreciated.
Upvotes: 0
Views: 9974
Reputation: 11
I overcame this problem by doing the following: signing the getters and setters methods in the main class! ATTENTION: if you try to sign using Lombok it is very likely that it won't work so by that I mean that you have to sign these methods and not using lombok writing Getter and Setter on top of the main class!
Upvotes: 1
Reputation: 468
If you can get the property by changing it from private to public, it sounds like your getters have problem. You should check your getters and setters in the Subscriber class.
If the getter is getFirstName(), it wouldn't work because the property name in the class is "firstname" and not firstName.
@Entity
@Table(name= "subscribers")
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 long getId(){
return this.id;
}
public void setId(long id){
this.id = id;
}
//This should not be getFirstName()
public String getFirstname(){
return this.firstname;
}
public void setFirstname(String fistname){
this.firstname = firstname;
}
//This should not be getLastName()
public String getLastname(){
return this.lastname;
}
public void setLastname(String lastname){
this.lastname = lastname;
}
public String getEmail(){
return this.email;
}
public void setEmail(String email){
this.email = email;
}
public Subscriber(){
}
}
Call these in thymeleaf:
<tr th:each="subscriber : ${subscribers}">
<td th:text="${subscriber.firstname} + ' ' + ${subscriber.lastname}"></td>
<td th:text="${subscriber.email}"></td>
I haven't tested these, but these should work.
Upvotes: 3
Reputation: 486
Thymeleaf uses getter methods in the view layer. When you say subscriber.firstName
, it calls subscriber.getFirstName()
. So have the getters with public
access modifier in Subscriber
class.
Upvotes: 0
Reputation: 12734
A little bit strange that it works if you have public field scope. But what I see is that your expression is not correct.
Try to use <td th:text="${subscriber.firstname + ' ' + subscriber.lastname}"></td>
Upvotes: 0