简体   繁体   中英

Neither BindingResult nor plain target object for bean name available as request attribute

URL (tried in Browser) :ip:port/Spring3MVC/studentRegistration.jsp

If I remove all the inputs from form:input it says no error.I tried other solutions provided here for same question but no use.Please help to narrow down the issue.Thanks in Advance.

Exception Trace :

SEVERE: Neither BindingResult nor plain target object for bean name 'student' available as request attribute
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'student' available as request attribute
    at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:141)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:174)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:194)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:160)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.autogenerateId(AbstractDataBoundFormElementTag.java:147)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.resolveId(AbstractDataBoundFormElementTag.java:138)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.writeDefaultAttributes(AbstractDataBoundFormElementTag.java:122)
    at org.springframework.web.servlet.tags.form.AbstractHtmlElementTag.writeDefaultAttributes(AbstractHtmlElementTag.java:409)
    at org.springframework.web.servlet.tags.form.InputTag.writeTagContent(InputTag.java:140)
    at org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:102)
    at org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:79)
    at org.apache.jsp.studentRegistration_jsp._jspx_meth_form_005finput_005f0(studentRegistration_jsp.java:144)
    at org.apache.jsp.studentRegistration_jsp._jspx_meth_form_005fform_005f0(studentRegistration_jsp.java:99)
    at org.apache.jsp.studentRegistration_jsp._jspService(studentRegistration_jsp.java:63)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)

Controller :

@Controller
public class HelloWorldController {
    List<Student> students = new ArrayList<Student>();
    @RequestMapping(method = RequestMethod.GET,value="/addStudent")
    public String addStudent(Model model,@ModelAttribute("student") Student student)
    {
        students.add(student);
        model.addAttribute("student", student);
        return "studentAddedOk";
    }
}

studentRegistration.jsp

<%@ page contentType="text/html"  %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>

<form:form commandName = "student" modelAttribute = "student" action="addStudent">
    <form:input path="name"/>
    <form:input path="age"/>
    <form:input path="qualification"/>
    <input type="submit" value="submit"/>
</form:form>

spring3-context.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">

    <bean id="student" class="spring.beans.Student" />

</beans> 

spring3-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">


    <context:component-scan base-package="spring.controller" />

    <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

</beans>

Student.java

package spring.beans;
public class Student {
    private String name;
    private String age;
    private String qualification;
    // getters and setters
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">

    <servlet>
        <servlet-name>spring3</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring3</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <context-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>/WEB-INF/spring3-context.xml</param-value>
    </context-param>
    <listener>
    <listener-class>
     org.springframework.web.context.ContextLoaderListener
    </listener-class>
    </listener>
</web-app>

In the controller, you need to add the student object as an attribute of the model :

model.addAttribute("student", new Student());

In controller where you are calling student view. like

@RequestMapping(value = "/", method = RequestMethod.GET) 
public String displayStudent( Model model) { 
 model.addAttribute("student", new Student()); 
 return "student"; 
} 

If the above is already done and you have not posted the code, try removing the commandName attribute from your form tag, the modelAttribute attribute is enough

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM