4
美好的一天。我遇到了一個問題:我試圖發送帶有一些屬性的POST請求,但是我收到了「錯誤的請求」響應。Spring MVC @ModelAttribute方法返回「錯誤請求」400
這裏是我的控制器:
@Controller
@RequestMapping("/group")
public class GroupController {
private static org.apache.log4j.Logger logger = org.apache.log4j.Logger
.getLogger(GroupController.class);
@Autowired
private GroupService groupService;
@RequestMapping(value = "/add", method = RequestMethod.POST)
public String addGroup(@ModelAttribute("group") final Group group) {
GroupController.logger.info("I'm in POST method");
this.groupService.addGroup(group);
return "redirect:/student/add";
}
}
這裏是我的實體:
@Entity
@Table(name = "university_groups")
public class Group implements Serializable {
private static final long serialVersionUID = 1L;
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "group")
Set<Student> students;
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@NotNull
@JoinColumn(name = "department_id")
private Department department;
@Id
@Column(name = "group_name", unique = true, nullable = false)
private String group;
public Group() {
}
public Group(final String group, final Department deparment) {
this.group = group;
this.department = deparment;
}
public Department getDepartment() {
return this.department;
}
public String getGroup() {
return this.group;
}
public Set<Student> getStudents() {
return this.students;
}
public void setDepartment(final Department department) {
this.department = department;
}
public void setGroup(final String group) {
this.group = group;
}
public void setStudents(final Set<Student> students) {
this.students = students;
}
@Override
public String toString() {
return this.group;
}
}
這裏是JSP頁面的一部分:
<form:form method="POST" action="add" commandName="group">
<table>
<tr>
<td><form:label path="group">
<spring:message code="label.student.group" />
</form:label></td>
<td><form:input path="group" /></td>
</tr>
<tr>
<td><form:label path="department">
<spring:message code="label.student.department" />
</form:label></td>
<td><form:select path="department">
<form:options items="${departments}" />
</form:select></td>
<td><a href="<c:url value="/department/add"/>"><spring:message
code="label.student.addDepartment" /></a></td>
</tr>
<tr>
<td colspan="2"><input type="submit"
value="<spring:message code="label.student.addGroup"/>" /></td>
</tr>
</table>
</form:form>
這裏是Department實體:
@Entity
@Table(name = "departments")
public class Department implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "department", unique = true, nullable = false)
private String department;
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@NotNull
@JoinColumn(name = "faculty_id")
private Faculty faculty;
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "department")
private Set<Group> groups;
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "users_departments", joinColumns = {@JoinColumn(name = "department_id", nullable = false, insertable = true, updatable = true) }, inverseJoinColumns = {@JoinColumn(name = "user_name", nullable = false, insertable = true, updatable = true) })
private Set<User> users;
public Department() {
}
public Department(final String department, final Faculty faculty) {
this.department = department;
this.faculty = faculty;
}
public String getDepartment() {
return this.department;
}
public Faculty getFaculty() {
return this.faculty;
}
public Set<Group> getGroups() {
return this.groups;
}
public Set<User> getUsers() {
return this.users;
}
public void setDepartment(final String department) {
this.department = department;
}
public void setFaculty(final Faculty faculty) {
this.faculty = faculty;
}
public void setGroups(final Set<Group> groups) {
this.groups = groups;
}
public void setUsers(final Set<User> users) {
this.users = users;
}
@Override
public String toString() {
return this.department;
}
}
如果我從控制器方法中刪除@ModelAttribute(「group」)最後一個Group Group,一切正常。 請幫助我,我不明白爲什麼它不起作用。
表單提交給'/ add',但應該是'/ group/add',不是嗎? –
@FranMontero如果是這種情況,那麼返回代碼應該是404而不是400.對嗎? –
@Nikolai粘貼服務器端拋出的異常。沒有它就很難猜測。也沒有部門類的代碼。 –