HTTP状态400 –错误的请求类型状态报告
说明由于以下原因,服务器无法或不会处理请求: 被视为客户错误的内容(例如,格式错误 请求语法,无效的请求消息框架或欺骗性请求 路由)。
我是Spring MVC的新手,学习@modelattribute注释并克服了错误。在没有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>
答案 0 :(得分:0)
您可能需要用@Controller而不是@Controller注释类。一个有用的页面,提供了使用@ ModelAttribute-- http://www.baeldung.com/spring-mvc-and-the-modelattribute-annotation --comments的示例:
将相应的班级注释为 @ControllerAdvice。因此,您可以在Model中添加值 确定为全球。这实际上意味着对于每个请求 响应部分中的每个方法都存在默认值。
也许您没有在Student类中添加所有属性?无论如何,由于没有使用@ControllerAdvice,因此Student的默认对象可能不可用。我希望这有帮助。但是无论如何,可以在我上面引用的那一页上找到关于该主题的更有用的文档。