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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13986136/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 14:39:10  来源:igfitidea点击:

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

javaspringbinding

提问by Chandrasekar

I am getting this error

我收到此错误

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

This is the stack trace

这是堆栈跟踪

org.apache.jasper.JasperException: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'command' available as request attribute org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:534) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:452) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333) 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.(BindStatus.java:141) org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:178) org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:198) org.springframework.web.servlet.tags.form.LabelTag.autogenerateFor(LabelTag.java:129) org.springframework.web.servlet.tags.form.LabelTag.resolveFor(LabelTag.java:119) org.springframework.web.servlet.tags.form.LabelTag.writeTagContent(LabelTag.java:89) org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:102) org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:79) org.apache.jsp.student_jsp._jspx_meth_form_005flabel_005f0(student_jsp.java:182) org.apache.jsp.student_jsp._jspx_meth_form_005fform_005f0(student_jsp.java:117) org.apache.jsp.student_jsp._jspService(student_jsp.java:79) 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:389) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333) javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.jasper.JasperException: java.lang.IllegalStateException: bean name 'command' 的 BindingResult 和普通目标对象都不能用作请求属性 org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:534) org. apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:452) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389) org.apache.jasper.servlet.JspServlet.service(JspServlet.java: 333) javax.servlet.http.HttpServlet.service(HttpServlet.java:722) 根本原因 java.lang.IllegalStateException: BindingResult 和 bean 名称“command”的普通目标对象都不能用作请求属性 org.springframework.web.servlet。 support.BindStatus.(BindStatus.java:141) org.springframework.web.servlet.tags.form。AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:178) org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:198) org.springframework.web.servlet.ags.form. java:129) org.springframework.web.servlet.tags.form.LabelTag.resolveFor(LabelTag.java:119) org.springframework.web.servlet.tags.form.LabelTag.writeTagContent(LabelTag.java:89) org. springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:102) org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:79) org.apache.jsp.student_jsp._0px05f(AbstractFormTag.java:102) student_jsp.java:182) org.apache.jsp.student_jsp。_jspx_meth_form_005fform_005f0(student_jsp.java:117) org.apache.jsp.student_jsp._jspService(student_jsp.java:79) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) javaxHttpServlet服务(HttpServlet.java:722) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389) org.apache.jasper。 servlet.JspServlet.service(JspServlet.java:333) javax.servlet.http.HttpServlet.service(HttpServlet.java:722)jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333) javax.servlet.http.HttpServlet.service(HttpServlet.java:722)jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333) javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

This is my web.xml file

这是我的 web.xml 文件

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

    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">


    <display-name>Spring MVC Form Handling</display-name>
    <welcome-file-list>
    <welcome-file>student.jsp</welcome-file>
  </welcome-file-list>

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>


    <servlet>
        <servlet-name>HelloWeb</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/HelloWeb-Servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>   

    <servlet-mapping>
        <servlet-name>HelloWeb</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

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

    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">


    <display-name>Spring MVC Form Handling</display-name>
    <welcome-file-list>
    <welcome-file>student.jsp</welcome-file>
  </welcome-file-list>

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>


    <servlet>
        <servlet-name>HelloWeb</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/HelloWeb-Servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>   

    <servlet-mapping>
        <servlet-name>HelloWeb</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

This is my applicationContext.xml file

这是我的 applicationContext.xml 文件

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

   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
   http://www.springframework.org/schema/mvc
   http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

   <context:component-scan base-package="com.Prime" />
<mvc:annotation-driven/>
   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/JSP/" />
      <property name="suffix" value=".jsp" />
   </bean>



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

   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
   http://www.springframework.org/schema/mvc
   http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

   <context:component-scan base-package="com.Prime" />
<mvc:annotation-driven/>
   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/JSP/" />
      <property name="suffix" value=".jsp" />
   </bean>



</beans>

This is my student.jsp file

这是我的 student.jsp 文件

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@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 modelAttribute="SpringWeb" method="POST" action="/HelloWeb/addStudent" commandName="SpringWeb">
   <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>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@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 modelAttribute="SpringWeb" method="POST" action="/HelloWeb/addStudent" commandName="SpringWeb">
   <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>

And this is my StudentController.java file

这是我的 StudentController.java 文件

package com.Prime;

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

@Controller
public class StudentController {

   @RequestMapping(value = "/student", method = RequestMethod.GET)
   public ModelAndView student() {
      return new ModelAndView("/student.jsp", "SpringWeb", 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";
   }
}
package com.Prime;

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

@Controller
public class StudentController {

   @RequestMapping(value = "/student", method = RequestMethod.GET)
   public ModelAndView student() {
      return new ModelAndView("/student.jsp", "SpringWeb", 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";
   }
}

Can anyone help me with this error.... Thanks in advance

任何人都可以帮我解决这个错误......提前致谢

采纳答案by Logarith

I tried your configuration and I didnt get your error. But I had to change something.

我尝试了您的配置,但没有收到您的错误消息。但我不得不改变一些东西。

The viewResolver is adding .jsp to your view name. So IMHO you should change

viewResolver 将 .jsp 添加到您的视图名称中。所以恕我直言,你应该改变

 return new ModelAndView("/student.jsp", "SpringWeb", new Student());

to

 return new ModelAndView("student", "SpringWeb", new Student());

So remove jsp and the "/" before student.

所以在student之前删除jsp和“/”。

Then it worked for me. But as I said I didnt get your error

然后它对我有用。但正如我所说,我没有得到你的错误

回答by Ralph

Edit:

编辑:

It brothers me that your exceptions states "bean name 'command'". I think you have some delopyment issue, because the code you posted here does not match the exception.

我兄弟,你的例外声明“bean name 'command'”。我认为您有一些部署问题,因为您在此处发布的代码与异常不匹配。



Remove the commandNameattribute from your form tag, the modelAttributeattribute is enought:

commandName从表单标签中删除属性,该modelAttribute属性就足够了:

<form:form modelAttribute="SpringWeb" method="POST" action="/HelloWeb/addStudent" >

After reading this article, it seams that this is not the solution for your problem, but anyway you do not need to specifiy them twice.

阅读这篇文章后,看来这不是您问题的解决方案,但无论如何您都不需要指定它们两次。

回答by Kiran

The jsp location the call to /studentis looking for is

调用要/student查找的 jsp 位置是

/WEB-INF/JSP//student.jsp.jsp

Change the configuration to match proper url.

更改配置以匹配正确的 url。