NotReadablePropertyException Bean属性不可读或具有无效的getter方法

时间:2015-02-19 23:05:00

标签: java spring jsp spring-form

出错: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;
    }

UPDATE2:

commandName="team"是否限制使用超过1个模型属性,在这种情况下有两个:organizationsteam?如何使它工作?

  

自定义属性名称:commandName描述:模型的名称   表单对象公开的属性。默认为   &#39;命令&#39 ;.必需:false可以具有运行时值:true

1 个答案:

答案 0 :(得分:0)

<form:option>用于将单个选项附加到选择列表。您可以使用<form:options>&#34;

添加集合中的所有元素
<form:select path="organization">
    <form:options items="${organizations}" />
</form:select>

您也可以同时使用两者,例如添加默认的未选择选项:

<form:select path="organization">
    <form:option value="" label="- Select -"/>
    <form:options items="${organizations}" />
</form:select>

将被选中的值将以通过表单传递的Team bean结束; path属性用于确定将在Team中设置哪个属性,在这种情况下,它是organisation(不是组织)