【发布时间】:2015-02-19 23:04:59
【问题描述】:
有错误: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?
更新:
我使用控制器将 所有组织 和新 Team 对象的列表传递给 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?如何让它发挥作用?
自定义属性名称:commandName 描述:模型名称 暴露表单对象的属性。默认为 '命令'。必需:false 可以有运行时值:true
【问题讨论】:
-
当你说所有组织时,你是什么意思?
-
您似乎正在尝试从 Team 类中获取组织属性;但是 Team 类只有(一个)组织。 organizations 属性不存在(这就是异常消息告诉您的内容)。
-
为什么
Teambean 需要form? -
我使用表单添加新的
Team对象并存储它。我必须为commandName属性指定一些东西...
标签: java spring jsp spring-form