【问题标题】:Field or property cannot be found on object of type 'org - Thymeleaf-Spring在“org - Thymeleaf-Spring”类型的对象上找不到字段或属性
【发布时间】: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


【解决方案1】:

如果您可以通过将其从私有更改为公共来获取该属性,那么听起来您的 getter 有问题。您应该在 Subscriber 类中检查您的 getter 和 setter。
如果 getter 是 getFirstName(),它将不起作用,因为类中的属性名称是“firstname”而不是 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(){
    }
}

在百里香中调用这些:

<tr th:each="subscriber : ${subscribers}">
<td th:text="${subscriber.firstname} + ' ' + ${subscriber.lastname}"></td>
<td th:text="${subscriber.email}"></td>

我没有测试过这些,但这些应该可以工作。

【讨论】:

    【解决方案2】:

    有点奇怪,如果您有公共字段范围,它会起作用。但我看到的是你的表达不正确。

    尝试使用&lt;td th:text="${subscriber.firstname + ' ' + subscriber.lastname}"&gt;&lt;/td&gt;

    【讨论】:

      【解决方案3】:

      Thymeleaf 在视图层使用 getter 方法。当你说subscriber.firstName 时,它会调用subscriber.getFirstName()。在Subscriber 类中使用具有public 访问修饰符的getter。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-11
        • 2020-09-22
        • 2019-12-12
        • 2022-06-29
        • 2018-01-09
        相关资源
        最近更新 更多