使用c:foreach"items"的NotReadablePropertyException; Spring形式的路径属性中的变量:复选框 [英] NotReadablePropertyException using c:foreach "items" variable in path attribute of Spring form:checkbox
问题描述
背景-根据给定的筛选条件(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"中查找属性.但这不是我想要的. 高度赞赏有关如何解决此问题的建议!
创建一个简单的包装对象以包含您的服务列表: 然后将包装器添加到模型中: 然后,您的JSP应该可以按预期工作,而无需进行修改,但是您也可以删除单独的 (请注意,首先不是问题是 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) - In Controller class - 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-2 It gives 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! Create a simple wrapper object to contain your list of services: And add the wrapper to the model instead: Your JSP should then work as intended without modification, but you can also remove the separate (note that it's not the 这篇关于使用c:foreach"items"的NotReadablePropertyException; Spring形式的路径属性中的变量:复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!<form:form>
希望在单个命令"对象(由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));
servicesOffered
模型属性,并将JSP循环更改为:<c:forEach var="service" items="${selectedService.servicesOffered}" varStatus="row">
forEach
-如果您没有forEach
但尝试了path="servicesOffered[0].monday"
,您将遇到同样的问题)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;
@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";
}
<form:form id="filterUsersForm" method="post" action="services.html" modelAttribute="searchCriteria">
/*some code here */
<input type="submit"..>
</form:form>
<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:form>
expects to work on a single "command" object (specified by the modelAttribute
attribute), and paths are all relative to this object.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));
servicesOffered
model attribute and change the JSP loop to:<c:forEach var="service" items="${selectedService.servicesOffered}" varStatus="row">
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)