java - Thymeleaf 不适用于 lombok forEach

标签 java spring tomcat thymeleaf lombok

我在 spring boot 项目中遇到 Thymeleaf“forEach”和 Lombok 项目的问题。 如果我将生成 getter 和 setter,那么一切正常。如果我使用 lombok 注释,我会收到此异常:

EL1008E: Property or field 'firstName' cannot be found on object of type 'pl.codol.hibernate.model.CustomerEntity' - maybe not public or not valid?

有谁知道什么是错的?我阅读了其他主题,但它们并没有帮助我解决问题。

我的 POJO 类:

@Data // I also used @Getter and @Setter, doesn't work
@NoArgsConstructor
@Entity
@Table(name = "CUSTOMER")
public class CustomerEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "CUSTOMER_ID")
    private Long id;

    @Column(name = "FIRST_NAME")
    private String firstName;

    @Column(name = "LAST_NAME")
    private String lastName;

    @Column(name = "EMAIL")
    private String email;

    @Override
    public String toString() {
        return "CustomerEntity{" +
                "id=" + id +
                ", firstName='" + firstName + '\'' +
                ", lastName='" + lastName + '\'' +
                ", email='" + email + '\'' +
                '}';
    }
}

Controller :

@Controller
@RequestMapping("/customer")
public class CustomerController {

    private CustomerService customerService;

    @Autowired
    public CustomerController(CustomerService customerService) {
        this.customerService = customerService;
    }

    @RequestMapping("/list")
    public String listCustomers(Model model) {
        List<CustomerEntity> allCustomers = customerService.findAllCustomers();
        model.addAttribute("customers", allCustomers);
        return "list-customers";
    }
}

导致问题的部分html文件:

<th:block th:each="customer : ${customers}">
<tr>
<td th:text="${customer.firstName}">...</td>
<td th:text="${customer.lastName}">...</td>
<td th:text="${customer.email}">...</td>
</tr>
</th:block> 

最佳答案

我已经发现了一个问题。

问题是在 Intellij 中缺少启用注释处理的复选框。

关于java - Thymeleaf 不适用于 lombok forEach,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53217364/

相关文章:

java - Spring-data 无法更新 id 为 0 的对象

java - 如何从数组末尾删除一个元素并将其插入到前面

java - 在 Quartz-Scheduler 中创建 bean 时出错

java - 使用 Java 类中的 file.properties 参数访问属性

tomcat - 从 NetBeans : '127.0.0.1*' is not recognized as an internal or external command 启动 Tomcat 时出错

java - 在 Quarkus 中重定向到 https

java - Spring 如何构造传递给 Controller ​​方法的对象?

Spring 5 响应式(Reactive)异常处理

java - 如何访问/myservlet 示例 servlet?

java - 当上下文路径为 '' 时,application.getContext() 返回 null