NotReadablePropertyException using c:foreach “items” variable in path attribute of Spring form:checkbox

这一生的挚爱 提交于 2020-12-12 09:49:23

问题


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!


回答1:


<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)



来源:https://stackoverflow.com/questions/32665916/notreadablepropertyexception-using-cforeach-items-variable-in-path-attribute

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!