user3825086
user3825086

Reputation: 111

Error in jsp page in using spring mvc

'm working on a Spring 3.1 MVC application, and my JSP won't come up. I am getting the following in my browser screen.

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

my controller

            package rugal.sample.common;

            import org.springframework.ui.ModelMap;
            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;

            public class StudentController
            {
                 @RequestMapping(value = "/student", method = RequestMethod.GET)
                   public ModelAndView student() {
                      return new ModelAndView("student", "command", new Student());
                   }

                   @RequestMapping(value = "/addStudent", method = RequestMethod.POST)
                   public String addStudent(@ModelAttribute("SpringWeb")Student student, 
                   ModelMap model) {
                      model.addAttribute("name", student.getName());
                      model.addAttribute("age", student.getAge());
                      model.addAttribute("id", student.getId());

                      return "result";
                   }
            }

Student.jsp

        <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
        <html>
        <head>
            <title>Spring MVC Form Handling</title>
        </head>
        <body>

        <h2>Student Information</h2>
        <form:form method="POST" action="/HelloWeb-servlet/addStudent">
           <table>
            <tr>
                <td><form:label path="name">Name</form:label></td>
                <td><form:input path="name" /></td>
            </tr>
            <tr>
                <td><form:label path="age">Age</form:label></td>
                <td><form:input path="age" /></td>
            </tr>
            <tr>
                <td><form:label path="id">id</form:label></td>
                <td><form:input path="id" /></td>
            </tr>
            <tr>
                <td colspan="2">
                    <input type="submit" value="Submit"/>
                </td>
            </tr>
        </table>  
        </form:form>
        </body>
        </html>


        ----------




    HelloWeb-servlet


    package rugal.sample.common;

    import org.springframework.ui.ModelMap;
    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;

    public class StudentController
    {
         @RequestMapping(value = "/student", method = RequestMethod.GET)
           public ModelAndView student() {
              return new ModelAndView("student", "command", new Student());
           }

           @RequestMapping(value = "/addStudent", method = RequestMethod.POST)
           public String addStudent(@ModelAttribute("SpringWeb")Student student, 
           ModelMap model) {
              model.addAttribute("name", student.getName());
              model.addAttribute("age", student.getAge());
              model.addAttribute("id", student.getId());

              return "result";
           }
    }



ERROR:

org.apache.jasper.JasperException: An exception occurred processing JSP page /WEB-INF/jsp/student.jsp at line 12

9: <form:form method="POST" action="/HelloWeb/addStudent">
10:    <table>
11:     <tr>
12:         <td><form:label path="name">Name</form:label></td>
13:         <td><form:input path="name" /></td>
14:     </tr>
15:     <tr>

Stacktrace:

    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:553)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:452)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
root cause

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'command' available as request attribute
    org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:144)
    org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:168)
    org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:188)
    org.springframework.web.servlet.tags.form.LabelTag.autogenerateFor(LabelTag.java:130)
    org.springframework.web.servlet.tags.form.LabelTag.resolveFor(LabelTag.java:120)
    org.springframework.web.servlet.tags.form.LabelTag.writeTagContent(LabelTag.java:90)
    org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:84)
    org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:80)
    org.apache.jsp.WEB_002dINF.jsp.student_jsp._jspx_meth_form_005flabel_005f0(student_jsp.java:175)
    org.apache.jsp.WEB_002dINF.jsp.student_jsp._jspx_meth_form_005fform_005f0(student_jsp.java:110)
    org.apache.jsp.WEB_002dINF.jsp.student_jsp._jspService(student_jsp.java:72)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

Upvotes: 1

Views: 2118

Answers (2)

Rafik BELDI
Rafik BELDI

Reputation: 4158

when there is a request to render you Student.jsp the controller must handle that this way :

@RequestMapping(value = "/addStudent",method = RequestMethod.GET)
    public ModelAndView initView() {
        Student studentModel = new Student();

        return new ModelAndView("Student", "studentModel", studentModel );
    }

Then Handel The Post :

  @RequestMapping(value = "/addStudent", method = RequestMethod.POST)
                   public String addStudent(@ModelAttribute("studentModel")Student student, 
                   ModelMap model) {
                      model.addAttribute("name", student.getName());
                      model.addAttribute("age", student.getAge());
                      model.addAttribute("id", student.getId());
//hope you have a result.jsp if you don't then return the name of your desired page
                      return "result";
                   }

You should add the commandName studentModel to your form, and remove helloWeb-servlet from the action of the form:

<form:form method="post" action="/addStudent" commandName="studentModel">

and make sure that the name on your form inputs is an exact match with the attributes of your Student class

    Student.java
package rugal.sample.common;


public class Student
{
    private Integer age;
       private String name;
       private Integer id;

       public void setAge(Integer age) {
          this.age = age;
       }
       public Integer getAge() {
          return age;
       }

       public void setName(String name) {
          this.name = name;
       }
       public String getName() {
          return name;
       }

       public void setId(Integer id) {
          this.id = id;
       }
       public Integer getId() {
          return id;
       }
}

EDIT check Student.jsp Line 12 remove : <form:label path="name">Name </form:label> and replace it by <label>Name</label>

Upvotes: 1

Atul Sharma
Atul Sharma

Reputation: 728

The controller mapping '/addStudent' is expecting a parameter of type 'Student' which needs to be passe d to the controller.Please create a pojo class named 'Student' and pass it to the controller. Use commandName inside the from tag to do this.

Upvotes: 1

Related Questions