我在 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/