Bean 属性 'categoryName' 不可读或具有无效的 getter 方法

Bean property 'categoryName' is not readable or has an invalid getter method

任何人都可以看看这个错误并帮助我吗?我花了 2 个小时来寻找问题,但没有找到解决方案。 列出数据有效,但问题是在使用 action="saveCategory.

添加 jsp 时

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

JSP:

<c:forEach var="tempInvoiceCategory" items="${invoiceCategory}">
    <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>

<form:form class="forms-sample" action="saveCategory" modelAttribute="invoiceCategory" 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>

实体:

@Entity
@Table(name="T_INVOICE_CATEGORY")
public class InvoiceCategory {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="ID_CATEGORY")
    private int id;
    
    @Column(name="CATEGORY_NAME")
    private String categoryName;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getCategoryName() {
        return categoryName;
    }

    public void setCategoryName(String categoryName) {
        this.categoryName = categoryName;
    }

    @Override
    public String toString() {
        return "InvoiceCategory [id=" + id + ", categoryName=" + categoryName + "]";
    }
    
}

控制器:

@Controller
@RequestMapping("/invoice")
public class InvoiceController {
    
    @Autowired
    private InvoiceCategoryService invoiceCategoryService;
    
    @GetMapping("/listCategory")
    public String listCategory(Model theModel) {
        
        List<InvoiceCategory> invoiceCategory = invoiceCategoryService.getInvoiceCategory();
        
        theModel.addAttribute("invoiceCategory",invoiceCategory);
        
        return "add-invoice-category";
    }
        
    @PostMapping("/saveCategory")
    public String saveCustomer(@ModelAttribute("invoiceCategory") InvoiceCategory theInvoiceCategory) {
        invoiceCategoryService.saveInvoiceCategory(theInvoiceCategory);
        
        return "redirect:/customer/list";
    }
}

我认为问题是模型属性 invoiceCategoryList<InvoiceCategory> 并且在同一个 JSP 中,您尝试构建项目列表和尝试访问该项目的表单,但它是 List<InvoiceCategory>:

<form:form class="forms-sample" action="saveCategory" modelAttribute="invoiceCategory" 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>

尝试评论此部分并再次 运行 申请。