Spring框架NotReadablePropertyException问题求助:Bean属性'categoryName'不可读或getter方法无效
Hey there, let's figure out why you're hitting this error and fix it right away.
What's causing the issue?
The error message spells it out clearly: Spring is trying to access the categoryName property on an ArrayList instead of your InvoiceCategory entity. Here's the root of the problem:
- When you load the
listCategorypage, you're adding a Listto the Model with the key invoiceCategory. - But your form uses
modelAttribute="invoiceCategory", which expects a singleInvoiceCategoryobject to bind form fields likecategoryNameto. - Since an ArrayList doesn't have a
getCategoryName()method, Spring throws thatNotReadablePropertyException.
Let's fix this step by step
1. Update your Controller's listCategory method
We need to separate the list of categories from the empty object used for the form. Rename the list's key and add a new empty InvoiceCategory for form binding:
@Controller @RequestMapping("/invoice") public class InvoiceController { @Autowired private InvoiceCategoryService invoiceCategoryService; @GetMapping("/listCategory") public String listCategory(Model theModel) { // Rename the list to avoid conflict with the form object List<InvoiceCategory> invoiceCategories = invoiceCategoryService.getInvoiceCategory(); theModel.addAttribute("invoiceCategories", invoiceCategories); // Add an empty InvoiceCategory for the form theModel.addAttribute("newInvoiceCategory", new InvoiceCategory()); return "add-invoice-category"; } @PostMapping("/saveCategory") // Match the model attribute name to the form's key public String saveCategory(@ModelAttribute("newInvoiceCategory") InvoiceCategory theInvoiceCategory) { invoiceCategoryService.saveInvoiceCategory(theInvoiceCategory); // Redirect back to the category list (fixed the likely typo from /customer/list) return "redirect:/invoice/listCategory"; } }
2. Adjust your JSP's list loop
Update the items attribute to use the new key for the category list:
<c:forEach var="tempInvoiceCategory" items="${invoiceCategories}"> <tr> <td>${tempInvoiceCategory.categoryName}</td> <td> <button type="button" class="btn btn-info">Edit</button> <button type="button" class="btn btn-danger">Delete</button> </td> </tr> </c:forEach>
3. Fix the form's modelAttribute
Change the form's modelAttribute to point to the empty InvoiceCategory object we added:
<form:form class="forms-sample" action="saveCategory" modelAttribute="newInvoiceCategory" method="POST"> <div class="form-group"> <label>Nazwa Kategorii</label> <form:input path="categoryName" type="text" class="form-control"/> </div> <button type="submit" class="btn btn-primary mr-2">Submit</button> </form:form>
Quick extra tips
I renamed the saveCustomer method to saveCategory since it's handling category data—this just makes your code more readable and avoids confusion. Also, the original redirect was pointing to /customer/list, which I switched to /invoice/listCategory assuming that's where you want users to land after saving a category.
内容的提问来源于stack exchange,提问作者Adrian Drohomirecki

