美好的一天。我遇到了一个问题:我尝试发送带有某些属性的POST请求,但是我得到了“请求错误”#34;响应。
这是我的控制器:
@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>
这是部门实体:
@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(&#34; group&#34;)最终的组组,则一切正常。 请帮助我,我无法理解它为什么不起作用。
答案 0 :(得分:4)
除了Juergen Hoeller之外,没有人完全理解@ModelAttribute
。好像他们创造它来吓跑新人一样。
他们确实需要将名称更改为@ReadModelAttribute
。
您不需要@ModelAttribute
参数注释只是为了将POJO用作参数。
您只需要出于以下原因之一:
flashAttribute
@SessionAttribute
@ModelAttribute method
(应重命名为@WriteModelAttribute
)由于您未执行任何操作,因此请勿在此处使用@ModelAttribute
参数注释。