URL(在浏览器中尝试):ip:port / Spring3MVC / studentRegistration.jsp
如果我从表单中删除所有输入:输入它说没有错误。我尝试了其他解决方案提供相同的问题,但没有用。请帮助缩小问题范围。谢谢提前。
异常追踪:
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
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>
答案 0 :(得分:1)
在控制器中,您需要将student
对象添加为attribute
的{{1}}:
model
在您呼叫model.addAttribute("student", new Student());
视图的controller
中。像
student
如果上述内容已经完成且您尚未发布代码,请尝试从表单标记中删除@RequestMapping(value = "/", method = RequestMethod.GET)
public String displayStudent( Model model) {
model.addAttribute("student", new Student());
return "student";
}
属性,commandName
属性已足够