Spring MVC @ModelAttribute Method Returns Bad Request 400

Good day. I ran into a problem: I'm trying to send a POST request with some attributes, but I get a "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 essence:

@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 the part of the 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 the department object:

@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 you delete the @ModelAttribute ("group") group, the final group of the group from the controller method, everything is fine. Please help me, I do not understand why this is not working.

+4
source share
1 answer

No one except Jürgen Huller fully understands @ModelAttribute. As if they created it to scare away new guys.

@ReadModelAttribute.

@ModelAttribute, POJO .

:

  • flashAttribute
  • @SessionAttribute
  • @ModelAttribute method ( @WriteModelAttribute)

, @ModelAttribute .

+4

Source: https://habr.com/ru/post/1606220/


All Articles