使用c:foreach"items"的NotReadablePropertyException; Spring形式的路径属性中的变量:复选框 [英] NotReadablePropertyException using c:foreach "items" variable in path attribute of Spring form:checkbox

查看:59
本文介绍了使用c:foreach"items"的NotReadablePropertyException; Spring形式的路径属性中的变量:复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景-根据给定的筛选条件(SearchCriteriaForm)填充的数据网格(ServiceOfferedForm列表).此外,每个数据行都有一个绑定到ServiceOfferedForm的ID字段的单选按钮,该单选按钮将填充在"selectedService"的serviceCode字段中,以用于编辑"操作.

我有一个如下的模型,带有吸气剂&设置器(此处未显示)-

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种表格-一种是表格过滤器&另一个用于编辑网格中的选定行(将打开一个新屏幕进行编辑)

Form-1

 <form:form id="filterUsersForm" method="post" action="services.html" modelAttribute="searchCriteria">
    /*some code here */
   <input type="submit"..>
</form: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"中查找属性.但这不是我想要的. 高度赞赏有关如何解决此问题的建议!

解决方案

<form:form>希望在单个命令"对象(由属性指定)上工作,并且路径都相对于该对象. /p>

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

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",您将遇到同样的问题)

Background - A data grid (list of ServiceOfferedForm) populated based on given filter criteria (SearchCriteriaForm). Also, each data row has a radiobutton bound to ID field of ServiceOfferedForm, which will be populated in serviceCode field of "selectedService", to be used for Edit action.

I have a Model as below, with getters & setters (not shown here) -

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;

In Controller class -

@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";
}

In my services.jsp, I have 2 forms - One for Grid Filter & another for Editing selected row from grid (a new screen will be opened for edit)

Form-1

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

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

Looks like it is trying to find the property within the modelAttribute "selectedService" given for Form-2, which is the instance of ServiceOfferedForm. But that is not what I want. Highly appreciate suggestions on how to solve this!

解决方案

<form:form> expects to work on a single "command" object (specified by the modelAttribute attribute), and paths are all relative to this object.

Create a simple wrapper object to contain your list of services:

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;
    }
}

And add the wrapper to the model instead:

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

Your JSP should then work as intended without modification, but you can also remove the separate servicesOffered model attribute and change the JSP loop to:

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

(note that it's not the forEach that is the problem in the first place - if you had no forEach but tried path="servicesOffered[0].monday" you would have the same issue)

这篇关于使用c:foreach"items"的NotReadablePropertyException; Spring形式的路径属性中的变量:复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆