gpt4 book ai didi

java - Spring MVC @ModelAttribute 方法返回 "Bad request"400

转载 作者:搜寻专家 更新时间:2023-11-01 02:41:23 24 4
gpt4 key购买 nike

美好的一天。我遇到了一个问题:我正在尝试发送带有某些属性的 POST 请求,但收到“错误请求”响应。

这是我的 Controller :

@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;
}

}

如果我从 Controller 方法中删除 @ModelAttribute("group") final Group 组,一切正常。请帮助我,我不明白为什么它不起作用。

最佳答案

除了 Juergen Hoeller 没有人完全理解 @ModelAttribute。就好像他们创造它是为了吓跑新人。

他们确实需要将名称更改为 @ReadModelAttribute

您不需要 @ModelAttribute 参数注释,只需使用 POJO 作为参数即可。

您仅出于以下原因之一需要它:

  1. 读取一个flashAttribute
  2. 读取一个@SessionAttribute
  3. 读取一个@ModelAttribute 方法(应该重命名为@WriteModelAttribute)

由于您没有执行任何这些操作,因此不要在此处使用 @ModelAttribute 参数注释。

关于java - Spring MVC @ModelAttribute 方法返回 "Bad request"400,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32434489/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com