0

I am getting this error continuously ,I have seen earlier posts in stackoverflow similar to this one but I was not able to figure out how to map commandName with my controller class. What is the real use of commandName.

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'student' available as request attribute

Register.jsp

           <form:form class="form-horizontal" style="margin-top: 45px"
                action="registeration.do" method="post" commandName="student">

                <div class="form-group row">

                    <div class="col-9 col-sm-offset-2 col-lg-8">
                        <form:input type="text" class="form-control" id="email"
                            placeholder="Enter email" path="email"/>
                            <form:errors path="email" cssClass="error" />
                    </div>
                </div>

                <div class="form-group row">

                    <div class="col-9 col-sm-offset-2 col-lg-8">
                        <form:input type="password" class="form-control" id="password"
                            placeholder="Enter password" path="password" />
                            <form:errors path="password" cssClass="error" />
                    </div>
                </div>
                <div class="form-group row">

                    <div class="col-9 col-sm-offset-2 col-lg-8">
                        <form:input type="text" class="form-control"         id="name"
                            placeholder="Enter name" path="name"/>
                            <form:errors path="name" cssClass="error" />
                    </div>
                </div>

StudentController.java

       @Controller
       public class StudentController {



@RequestMapping(value="registeration.do" ,method=RequestMethod.POST)
public ModelAndView submitAdmissionForm(@Valid @ModelAttribute("student") Student student,BindingResult result,ModelMap model)
{
    System.out.println("Implementing the Validations");

    Map <String,Object> modelMap=new HashMap<String,Object>();
    modelMap.put("student", student);
    if(result.hasErrors())
    {
        System.out.println("error is there");



        return new ModelAndView("Registeration",modelMap); 

    }
    else{

        return new ModelAndView("AdmissionSuccess",modelMap); 


    }
}
}
}
SwagDevelopers
  • 109
  • 2
  • 12
  • 1
    Perhaps if you drop table swag, and read some spring-documentation, this would be clear... :-) Anyways, it is necessary for binding between your Controller method and your JSP, so user can input appropriate variables for the given class(part of command), and if necessary, validation checks can be triggered in controller itself. – We are Borg Dec 10 '15 at 08:54
  • Binding between Controller method and JSP can be done with "action" . I have done this with action only and as I try to add validation in my project it arises me with issue. – SwagDevelopers Dec 10 '15 at 09:01

1 Answers1

0

Whenever you want to link a POJO object with a HTML form, you have to pass a name in returning ModelAndView Object and give same name in HTML form (as you have used commandName="student").

In your case you are not passing it in returning ModelAndView object but you are using it in HTML form, That's why it is giving you error.

You should use ModelMap to put objects like "Registeration" and make returning object as :

ModelAndView("jspname","student", new StudentBean()); 

AND

new ModelAndView("AdmissionSuccess","student", new StudentBean());

Please refer constructors of ModelAndView Class