NotReadablePropertyException在Spring窗体的路径属性中使用c:foreach“items”变量:复选框

时间:2015-09-19 08:14:43

标签: jsp spring-mvc

背景 - 基于给定的过滤条件(SearchCriteriaForm)填充的数据网格(ServiceOfferedForm列表)。此外,每个数据行都有一个绑定到ServiceOfferedForm的ID字段的radiobutton,它将填充在“selectedService”的serviceCode字段中,用于编辑操作。

我有一个如下的模型,有吸气剂和放大器setters(此处未显示) -

public class ServiceOfferedForm implements Serializable{       
private String serviceCode;    
private String serviceName;       
private Boolean monday;    
private Boolean tuesday;    
private Boolean wednesday;    
private Boolean thursday;    
private Boolean friday;    
private Boolean saturday;    
private Boolean sunday;

在Controller类中 -

@RequestMapping(value="/services.html", method=RequestMethod.GET)
public String initManageServices(Model model){  

    ArrayList<ServiceOfferedForm> services = /*Code to fetch from database*/
    model.addAttribute("servicesOffered",services);

    //Adding other required attributes to model
    model.addAttribute("searchCriteria",new SearchCriteriaForm());
    model.addAttribute("selectedService",new ServiceOfferedForm()); 

    return "services";
}

在我的services.jsp中,我有2个表单 - 一个用于Grid Filter&amp;另一个用于从网格编辑选定的行(将打开一个新屏幕进行编辑)

Form-1

 <form:form id="filterUsersForm" method="post" action="services.html" modelAttribute="searchCriteria">
    /*some code here */
   <input type="submit"..>
</form:form>

表格-2

    <form:form id="selectSvcFromGrid"  method="get" action="editService" modelAttribute="selectedService" >
    //Some code here for table header for grid
    <c:forEach var="service" items="${servicesOffered}" varStatus="row">
    <tr>
        <td><form:checkbox path="servicesOffered[${row.index}].monday" disabled="true"/></td>
        <td><form:checkbox path="servicesOffered[${row.index}].tuesday" disabled="true"/></td>
        <td><form:checkbox path="servicesOffered[${row.index}].wednesday" disabled="true"/></td>
        <td><form:checkbox path="servicesOffered[${row.index}].thursday" disabled="true"/></td>
        <td><form:checkbox path="servicesOffered[${row.index}].friday" disabled="true"/></td>
        <td><form:checkbox path="servicesOffered[${row.index}].saturday" disabled="true"/></td>
        <td><form:checkbox path="servicesOffered[${row.index}].sunday" disabled="true"/></td>                           
    </tr>
</c:forEach>

它提供org.springframework.beans.NotReadablePropertyException: Invalid property 'servicesOffered[0]' of bean class [com.tfts.form.ServiceOfferedForm]:

看起来它正在尝试在为Form-2(它是ServiceOfferedForm的实例)给出的modelAttribute“selectedService”中找到该属性。但这不是我想要的。 非常感谢有关如何解决此问题的建议!

1 个答案:

答案 0 :(得分:0)

<form:form>希望能够使用单个&#34;命令&#34; object(由modelAttribute属性指定),路径都是相对于此对象的。

创建一个简单的包装器对象以包含您的服务列表:

public class ServicesOfferedForm {
    private List<ServiceOfferedForm> servicesOffered;

    public ServicesOfferedForm(List<ServiceOfferedForm> servicesOffered) {
        this.servicesOffered = servicesOffered;
    }

    public List<ServiceOfferedForm> getServicesOffered() {
        return servicesOffered;
    }

    public void setServicesOffered(List<ServiceOfferedForm> servicesOffered) {
        this.servicesOffered = servicesOffered;
    }
}

然后将包装器添加到模型中:

model.addAttribute("selectedService", new ServicesOfferedForm(services));

您的JSP应该按预期工作而无需修改,但您也可以删除单独的servicesOffered模型属性并将JSP循环更改为:

<c:forEach var="service" items="${selectedService.servicesOffered}" varStatus="row">

(请注意,问题首先不是forEach - 如果您没有forEach但尝试path="servicesOffered[0].monday"则会遇到同样的问题)