问题描述
美好的一天。我遇到了一个问题:我试图发送带有一些属性的POST请求,但是我收到了错误的请求响应。这里是我的控制器:
@Controller
@RequestMapping(/ group)
public class GroupController {
private static org.apache.log4j.Logger logger = org.apache.log4j.Logger
.getLogger(GroupController 。类);
@Autowired
私人GroupService groupService;
@RequestMapping(value =/ add,method = RequestMethod.POST)
public String addGroup(@ModelAttribute(group)final Group group){
GroupController.logger.info(我在POST方法);
this.groupService.addGroup(group);
返回重定向:/学生/添加;
$ / code $ / pre
这是我的实体:
@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>学生们;
@ManyToOne(fetch = FetchType.EAGER,cascade = CascadeType.ALL)
@NotNull
@JoinColumn(name =department_id)
私人部门部门;
@Id
@Column(name =group_name,unique = true,nullable = false)
private String group;
公共集团(){
}
公共集团(最终字符串组,最终部门部门){
this.group = group;
this.department = deparment;
}
public Department getDepartment(){
return this.department;
}
public String getGroup(){
return this.group;
}
public Set< Student> getStudents(){
返回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 =POSTaction =addcommandName =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>
以下是部门实体:
<$ p $
@Table(name =departments)
public class Department实现可序列化{
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)
私立教职员;
@OneToMany(fetch = FetchType.EAGER,cascade = CascadeType.ALL,mappedBy =department)
private Set< Group>组;
@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>用户;
公共部门(){
}
公共部门(最终字符串部门,最终教职员工){
这个部门=部门;
this.faculty = faculty;
}
public String getDepartment(){
return this.department;
}
公立学院getFaculty(){
return this.faculty;
}
public Set< Group> getGroups(){
return this.groups;
}
public Set< User> getUsers(){
返回this.users;
}
public void setDepartment(final String department){
this.department = department;
}
public void setFaculty(最终教职员工){
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(组)来自控制器方法的最终组群,一切正常。
请帮助我,我不明白为什么它不起作用。
除了Juergen Hoeller完全理解 @ModelAttribute
。这就好像他们创造了它来吓跑新人。他们真的需要改名为 @ReadModelAttribute
。
您不需要 @ModelAttribute
参数注释就可以使用POJO作为参数。
由于以下原因之一,您只需要它:
- 阅读
flashAttribute
- 阅读
@SessionAttribute
阅读 - Read a
flashAttribute
- Read a
@SessionAttribute
- Read a
@ModelAttribute method
(which should be renamed to@WriteModelAttribute
)
@ModelAttribute方法
(应将其重命名为 @WriteModelAttribute
) 由于您没有执行其中的任何操作,因此请勿在此处使用 @ModelAttribute
参数注释。
Good day. I faced a problem: I'm trying to send POST request with some attributes, but I get "Bad request" response.
Here is my 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";
}
}
Here is my entity:
@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;
}
}
Here is part of JSP page:
<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>
Here is Department entity:
@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;
}
}
If I remove @ModelAttribute("group") final Group group from controller method, all is OK.Please, help me, I can't understand why it doesn't works.
No one except Juergen Hoeller understands @ModelAttribute
fully. It's as though they created it to scare away new guys.
They really need to change the name to @ReadModelAttribute
.
You don't need @ModelAttribute
parameter annotation just to use a POJO as a parameter.
You only need it for one of the following reasons:
Since you aren't doing any of those, don't use @ModelAttribute
parameter annotation here.
这篇关于Spring MVC @ModelAttribute方法返回“错误请求” 400的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!