控制器设置空的team
并获取所有organizations
,然后将这两个对象传递给JSP。由于某些原因,表单无法提交数据并导致:400 The request sent by the client was syntactically incorrect.
。 Chrome显示正在传递的表单数据:
name:Chris Paul Camps
rating:9
organization:com.sprhib.model.Organization@7ebffc91
JSP
<form:form method="POST" commandName="team" action="${pageContext.request.contextPath}/team/add.html" class="col-md-4">
<div class="form-group">
<label for="name"><spring:message code="label.name"></spring:message>:</label>
<form:input class="form-control" path="name" id="name" />
</div>
<div class="form-group">
<label for="rating"><spring:message code="label.rating"></spring:message>:</label>
<form:input class="form-control" path="rating" id="rating" />
</div>
<div class="form-group">
<label for="organization"><spring:message code="label.organization"></spring:message>:</label>
<form:select class="form-control" path="organization" id="organization">
<form:option value="" label="- Select -"/>
<form:options items="${organizations}" itemLabel="name" />
</form:select>
</div>
<input type="submit" value="<spring:message code="label.add"></spring:message>" class="btn btn-default"/>
</form:form>
控制者
@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;
}
@RequestMapping(value="/add", method=RequestMethod.POST)
public ModelAndView addingTeam(@ModelAttribute Team team) {
ModelAndView modelAndView = new ModelAndView("home");
teamService.addTeam(team);
String message = "Team was successfully added.";
modelAndView.addObject("message", message);
return modelAndView;
}
团队实体:
@Entity
@Table(name="teams")
public class Team {
private Integer id;
private String name;
private Integer rating;
private Set<Member> members;
private Organization organization;
@Id
@GeneratedValue
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getRating() {
return rating;
}
public void setRating(Integer rating) {
this.rating = rating;
}
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "FK_Organization_id", nullable = false)
public Organization getOrganization() {
return organization;
}
public void setOrganization(Organization organization) {
this.organization = organization;
}
@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinTable(name = "team_member", joinColumns =
@JoinColumn(name = "FK_Team_id", referencedColumnName= "id"),
inverseJoinColumns = @JoinColumn(name = "FK_Member_id", referencedColumnName = "id")
)
public Set<Member> getMembers() {
return members;
}
public void setMembers(Set<Member> members) {
this.members = members;
}
}
组织实体:
@Entity
@Table(name = "organization")
public class Organization {
private Integer id;
private String name;
private Set<Team> teams;
@Id
@GeneratedValue
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@OneToMany(fetch = FetchType.EAGER, mappedBy = "organization")
public Set<Team> getTeams() {
return teams;
}
public void setTeams(Set<Team> teams) {
this.teams = teams;
}
}
更新:
更改为以下错误后:
<form:select class="form-control" path="organization" id="organization">
<form:option value="" label="- Select -"/>
<form:options items="${organizations}" itemLabel="name" itemValue="id"/>
</form:select>
我是否必须在控制器中有两个
@ModelAttribute
,一个用于团队,另一个用于组织?UPDATE2:
给出以下错误,为什么
organization
似乎是null
? 最佳答案
尝试将控制器方法更改为
@RequestMapping(value="/add", method=RequestMethod.POST)
public ModelAndView addingTeam(@ModelAttribute Team team, BindingResult result) {
您最有可能发生绑定错误,但是由于在控制器内部您没有紧随@ModelAttribute之后的BindingResult,因此您收到了错误的请求
检查docs的BindingResult和@ModelAttribute的无效顺序。
Errors或BindingResult参数必须跟随模型对象
立即绑定,因为方法签名可能具有
不仅仅是一个模型
我确认,通过添加BindingResult,您将对失败的确切属性有更深入的了解
评论后更新
您还需要注册一个活页夹,它将转换您的组织,大致类似于
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(Organization.class,
new PropertyEditorSupport() {
@Override
public void setAsText(String text) {
Organization organization = dao.find(Organization.class,
Integer.parseInt(text));
setValue(organization);
}
});
}
dao.find(Organization.class, Integer.parseInt(text));
当然是元代码