gpt4 book ai didi

java - BindingResult 和 bean 名称的普通目标对象都不能作为请求属性

转载 作者:行者123 更新时间:2023-12-01 12:38:50 25 4
gpt4 key购买 nike

URL(在浏览器中尝试):ip:port/Spring3MVC/studentRegistration.jsp

如果我从 form:input 中删除所有输入,则表示没有错误。我针对同一问题尝试了此处提供的其他解决方案,但没有用。请帮助缩小问题范围。提前致谢。

异常跟踪:

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>

最佳答案

在 Controller 中,您需要添加 student 对象作为 modelattribute:

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

在您调用学生 View 的 Controller 中。喜欢

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

如果上述操作已经完成,并且您还没有发布代码,请尝试从表单标记中删除 commandName 属性,modelAttribute 属性就足够了

关于java - BindingResult 和 bean 名称的普通目标对象都不能作为请求属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25317660/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com