[英]Neither BindingResult nor plain target object for bean name available as request attribute
URL (tried in Browser) :ip:port/Spring3MVC/studentRegistration.jsp URL(在浏览器中尝试过):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. 如果我从form:input删除所有输入,则表示没有错误。我尝试了此处提供的其他解决方案来解决同样的问题,但没有用。请帮助缩小问题的范围。谢谢。
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 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 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 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 Student.java
package spring.beans;
public class Student {
private String name;
private String age;
private String qualification;
// getters and setters
}
web.xml 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
: 在控制器中,您需要将
student
对象添加为model
的attribute
:
model.addAttribute("student", new Student());
In controller
where you are calling student
view. 在
controller
,您正在调用student
视图。 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 如果上面的操作已经完成,但是您尚未发布代码,请尝试从表单标签中删除
commandName
属性, modelAttribute
属性就足够了
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.