一直获取:bean类的属性无效:bean属性不可读



无论我是手动创建getter/setter还是通过Lombok创建,我总是得到这个错误。

所以,我猜错误与getter/setter无关,但我找不到问题的答案。

这是我的错误信息:

Invalid property 'person' of bean class [java.util.ArrayList]: Bean property 'person' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?

这是我的实体类:

@Data
@Getter
@Setter
@Entity
@Table(name="incomeoutgo", schema = "public")
public class IncomeOutgo extends AbstractPersistable<Long> {
@Version
@NotNull
@Column(name ="id")
private Long id;
@Column(name="dayofweek")
private Date dayofweek;
@Column(name="location")
private String location;
@Size(min = 5, max = 50)
@Column(name ="person")
private String person;
@Min(0)
@Column(name ="version")
private Integer version;
@Column(name="income")
private int income;
@Column(name="outgo")
private int outgo;
}

这是控制器类

@RequiredArgsConstructor
@Controller
@RequestMapping(value = "/incomeoutgo")
public class IncomeOutgoController {
private static final String INCOMEOUTGO_VIEW = "incomeoutgo";
private final IncomOutgoService incomeoutgoService;
@GetMapping
public String showShop(Model model) {
List<IncomeOutgo> incomeOutgoList = incomeoutgoService.getIncomeOutgoList();
model.addAttribute(INCOMEOUTGO_VIEW, incomeOutgoList);
return INCOMEOUTGO_VIEW;
}
@PostMapping("/incomeoutgo")
public String addUser(@ModelAttribute("incomeoutgo") IncomeOutgo incomeoutgo, BindingResult bindingResult) {
incomeoutgoService.addIncomeOutgo(incomeoutgo);
return "incomeoutgo";
}
}

最后但并非最不重要的是我的thymleaf模板:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Incomes / Expenses</title>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
</head>
<body>
<table>
<tr>
<th>Id</th>
<th>Day of Week</th>
<th>Name</th>
<th>Location</th>
<th>Income</th>
<th>Expense</th>
</tr>
<tr th:each="incomeoutgo : ${incomeoutgo}">
<td th:text="${incomeoutgo.id}">id</td>
<td th:text="${incomeoutgo.dayofweek}">dayofweek</td>
<td th:text="${incomeoutgo.person}">person</td>
<td th:text="${incomeoutgo.location}">location</td>
<td th:text="${#numbers.formatCurrency(incomeoutgo.income)}">"${#numbers.formatCurrency(incomeoutgo.income)}"</td>
<td th:text="${#numbers.formatCurrency(incomeoutgo.outgo)}">"${#numbers.formatCurrency(incomeoutgo.outgo)}"</td>
</tr>
</table>
<form action="#" th:action="@{/incomeoutgo}" th:object="${incomeoutgo}" method="post">
<table>
<tr>
<td>Name:</td>
<td><input type="text" th:field="*{person}" /></td>
<td th:if="${#fields.hasErrors('person')}" th:errors="*{person}">Name Error</td>
</tr>
<tr>
<td><button type="submit">Submit</button></td>
</tr>
</table>
</form>
</body>
</html>

错误解释:Invalid property 'person' of bean class [java.util.ArrayList]。你试图在一个java.util.ArrayList上调用getPerson()(它没有这个属性)。我怀疑这是你的形式:

<form ... th:object="${incomeoutgo}">

您已经将incomeoutgo添加到您的模型中作为List<IncomeOutgo>,但您试图将其视为IncomeOutgo。您需要创建一个对象,并在尝试绑定表单字段值时使用它。

model.addAttribute(INCOMEOUTGO_VIEW, incomeOutgoList);
model.addAttribute("page", new IncomeOutgo());
.
.
.
<form ... th:object="${page}">
<input type="text" th:field="*{person}" />

(侧节点,多次重用相同的变量名会使代码混乱。您有模型属性${incomeoutgo},临时循环变量th:each="incomeoutgo : ${incomeoutgo}"和绑定的胸片对象th:object="${incomeoutgo}",它们可能相关,也可能不相关。

相关内容

最新更新