Search code examples
spring-mvcannotationsmodelattribute

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).

I am new in Spring MVC and learning about @modelattribute annotation and getting above error.Everything else works fine without modelattribute annotaion. I even tried using @RequestParam instead of @modelattribute and everthing worked fine. But I am not able to understand what is wrong with the code below. Please can anyone help?

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>


Solution

  • You may need to annotate your class not as @Controller but as @ControllerAdvice. A useful page providing an example of the use of @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.

    Perhaps you are not adding all attributes in your Student class? In any event, perhaps a default object for Student is not available since @ControllerAdvice is not being used. I hope this helps. But regardless, more useful documentation on the subject can be found on that page I referenced above.