You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring框架NotReadablePropertyException问题求助:Bean属性'categoryName'不可读或getter方法无效

解决Spring的NotReadablePropertyException问题

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 listCategory page, you're adding a List to the Model with the key invoiceCategory.
  • But your form uses modelAttribute="invoiceCategory", which expects a single InvoiceCategory object to bind form fields like categoryName to.
  • Since an ArrayList doesn't have a getCategoryName() method, Spring throws that NotReadablePropertyException.

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 11:37:29