Spring 方法参数中的 MVC @ModelAttribute 未将数据与 java 对象绑定

Spring MVC @ModelAttribute in method parameter not binding data with java object

HTTP Status 400 – Bad Request Type Status Report

Description The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).

我是 Spring MVC 的新手,正在学习 @modelattribute 注释并超越 error.Everything else 在没有 modelattribute 注释的情况下工作正常。我什至尝试使用 @RequestParam 而不是 @modelattribute 并且一切正常。但是我无法理解下面的代码有什么问题。请问有人能帮忙吗?

AdmissionController.java

    package Demo.SpringAnnotation;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class AdmissionController {
    @RequestMapping(value="/welcome",method=RequestMethod.GET)
    public ModelAndView formLoad() {
        ModelAndView mav = new ModelAndView("AdmssionForm");
        return mav;
    }
    @ModelAttribute
    public void commonHeaders(Model model1) {
        model1.addAttribute("headers", "New College of Engineering");
    } 

    @RequestMapping(value="/admissionsucess",method=RequestMethod.POST)
    public ModelAndView submitForm(@ModelAttribute("student1") Student student1) {

        ModelAndView mav=new ModelAndView("AmissionSucess");

        return mav;
    }
}

AdmissionForm.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Admission Form</title>
</head>
<body>
 <h2>${headers }</h2>
 <form action="/SpringAnnotation/admissionsucess" method="post">
  <table>
   <tr>
    <td>Name :</td>
    <td><input type="text" name="studentName"></td>
   </tr>
   <tr>
    <td>Hobbies:</td>
    <td><input type="text" name="studentHobby"></td>
   </tr>
   <tr>
    <td>Mobile:</td>
    <td><input type="text" name="studentMobile"></td>
   </tr>
   <tr>
    <td>Date Of birth:</td>
    <td><input type="text" name="studentHobby"></td>
   </tr>
   <tr>
    <td>Student Skills:</td>
    <td><select name="studentSkills" multiple >
    <option value="Java Core">Java Core</option>
    <option value="Spring Core">Spring Core</option>
    <option value="Spring MVC">Spring MVC</option>
    </select></td>
   </tr>
  </table>
  <button type="submit">Submit</button>
 </form>
</body>
</html>

AdmissionSuccess.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Admission Sucess</title>
</head>
<body>
 <h2>${headers }</h1>
  <h4>Congratulation ${student1.studentName }, Your details are below</h4>
  <table>
   <tr>
    <td>Name:</td>
    <td>${student1.studentName}</td>
   </tr>
   <tr>
    <td>Hobbies:</td>
    <td>${student1.studentHobby}</td>
   </tr>
   <tr>
    <td>Mobile:</td>
    <td>${student1.studentMobile}</td>
   </tr>
   <tr>
    <td>Date Of Birth:</td>
    <td>${student1.studentDOB}</td>
   </tr>
   <tr>
    <td>Name:</td>
    <td>${student1.studentSkills}</td>
   </tr>
  </table>
</body>
</html>

您可能需要将 class 注释为@ControllerAdvice 而不是@Controller。一个有用的页面,提供了使用 @ModelAttribute 的示例-- http://www.baeldung.com/spring-mvc-and-the-modelattribute-annotation --comments:

It is also important that you annotate the respective class as @ControllerAdvice. Thus, you can add values in Model which will be identified as global. This actually means that for every request a default value exists, for every method in the response part.

也许您没有在 Student 中添加所有属性 class?在任何情况下,由于未使用 @ControllerAdvice,Student 的默认对象可能不可用。我希望这有帮助。但无论如何,可以在我上面引用的那个页面上找到有关该主题的更多有用文档。