NotReadablePropertyException Bean 属性 不可读或具有无效的 getter 方法
NotReadablePropertyException Bean property is not readable or has an invalid getter method
有错误:org.springframework.beans.NotReadablePropertyException: Invalid property 'organizations' of bean class [com.sprhib.model.Team]: Bean property 'organizations' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
有表格:团队、组织。存在一对多关系。
团队模型
@Entity
@Table(name="teams")
public class Team {
private Organization organization;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "FK_Organization_id", nullable = false)
public Organization getOrganization() {
return organization;
}
public void setOrganization(Organization organization) {
this.organization = organization;
}
}
组织
@Entity
@Table(name = "organization")
public class Organization {
private Set<Team> teams;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "organization")
public Set<Team> getTeams() {
return teams;
}
public void setTeams(Set<Team> teams) {
this.teams = teams;
}
}
JSP
<form:form method="POST" commandName="team" action="${pageContext.request.contextPath}/team/add.html">
<form:select path="organizations">
<form:option value="${organization}">
<c:out value="${organization.name}"/>
</form:option>
</form:select>
</form:form>
如何spring让所有组织JSP?
更新:
我使用控制器将所有组织和新团队对象的列表传递给jsp:
@Controller
@RequestMapping(value="/team")
public class TeamController {
@Autowired
private TeamService teamService;
@Autowired
private OrganizationService organizationService;
@RequestMapping(value="/add", method=RequestMethod.GET)
public ModelAndView addTeamPage() {
ModelAndView modelAndView = new ModelAndView("teams/add-team-form");
modelAndView.addObject("team", new Team());
modelAndView.addObject("organizations", organizationService.getOrganizations());
return modelAndView;
}
更新 2:
commandName="team"
是否限制使用超过 1 个模型属性,在本例中有两个:organizations
和 team
?如何让它发挥作用?
Custom attribute name: commandName Description: Name of the model
attribute under which the form object is exposed. Defaults to
'command'. Required: false Can have runtime value: true
<form:option>
用于将单个选项附加到 select 列表。您可以使用 <form:options>
"
添加集合中的所有元素
<form:select path="organization">
<form:options items="${organizations}" />
</form:select>
您也可以同时使用两者,例如添加默认的未selected 选项:
<form:select path="organization">
<form:option value="" label="- Select -"/>
<form:options items="${organizations}" />
</form:select>
将被 selected 的值将在通过表单传递的 Team bean 中结束; path
属性用于确定将在团队中设置哪个 属性,在这种情况下它是 organisation
(不是组织)
有错误:org.springframework.beans.NotReadablePropertyException: Invalid property 'organizations' of bean class [com.sprhib.model.Team]: Bean property 'organizations' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
有表格:团队、组织。存在一对多关系。
团队模型
@Entity
@Table(name="teams")
public class Team {
private Organization organization;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "FK_Organization_id", nullable = false)
public Organization getOrganization() {
return organization;
}
public void setOrganization(Organization organization) {
this.organization = organization;
}
}
组织
@Entity
@Table(name = "organization")
public class Organization {
private Set<Team> teams;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "organization")
public Set<Team> getTeams() {
return teams;
}
public void setTeams(Set<Team> teams) {
this.teams = teams;
}
}
JSP
<form:form method="POST" commandName="team" action="${pageContext.request.contextPath}/team/add.html">
<form:select path="organizations">
<form:option value="${organization}">
<c:out value="${organization.name}"/>
</form:option>
</form:select>
</form:form>
如何spring让所有组织JSP?
更新:
我使用控制器将所有组织和新团队对象的列表传递给jsp:
@Controller
@RequestMapping(value="/team")
public class TeamController {
@Autowired
private TeamService teamService;
@Autowired
private OrganizationService organizationService;
@RequestMapping(value="/add", method=RequestMethod.GET)
public ModelAndView addTeamPage() {
ModelAndView modelAndView = new ModelAndView("teams/add-team-form");
modelAndView.addObject("team", new Team());
modelAndView.addObject("organizations", organizationService.getOrganizations());
return modelAndView;
}
更新 2:
commandName="team"
是否限制使用超过 1 个模型属性,在本例中有两个:organizations
和 team
?如何让它发挥作用?
Custom attribute name: commandName Description: Name of the model attribute under which the form object is exposed. Defaults to 'command'. Required: false Can have runtime value: true
<form:option>
用于将单个选项附加到 select 列表。您可以使用 <form:options>
"
<form:select path="organization">
<form:options items="${organizations}" />
</form:select>
您也可以同时使用两者,例如添加默认的未selected 选项:
<form:select path="organization">
<form:option value="" label="- Select -"/>
<form:options items="${organizations}" />
</form:select>
将被 selected 的值将在通过表单传递的 Team bean 中结束; path
属性用于确定将在团队中设置哪个 属性,在这种情况下它是 organisation
(不是组织)