当前位置: 首页 > 知识库问答 >
问题:

java.lang.IllegalStateExcture:BindingResult和bean名称'命令'的普通目标对象都不可用作请求属性[重复]

车嘉实
2023-03-14

我是SpringMVC框架的初学者。两天前我开始学习Spring。出于学习目的,我正在开发一个简单的应用程序。i、 例如,从表单中获取用户输入,并在另一个页面中显示值。我得到了一个异常“java.lang.IllegalStateException:BindingResult和bean名称'command'的普通目标对象都不能作为请求属性使用”。我不知道我的代码出了什么问题。我搜索了谷歌,尝试了很多解决方案,但问题仍然存在。

这是我的视图索引。jsp

<form:form action="/addDisplay" method="POST">
     <form:label path="name"><h3>Name</h3></form:label>
     <form:input type="text" path="name" cssClass="form-control text-center" required="required"/>

     <form:label path="age"><h3>Age</h3></form:label>
     <form:input type="number" path="age" cssClass="form-control text-center" required="required"/>

     <form:label path="work"><h3>Work Place</h3></form:label>
     <form:input type="text" path="work" cssClass="form-control text-center" required="required"/>

     <form:label path="designation"><h3>Designation</h3></form:label>
     <form:input type="text" path="designation" cssClass="form-control text-center" required="required"/>

     <form:label path="area"><h3>Area</h3></form:label>
     <form:input type="text" path="area" placeholder="Where Are You From?" cssClass="form-control text-center" required="required"/>

     <form:label path="mobile"><h3>Mobile Number</h3></form:label>
     <form:input type="number" path="mobile" placeholder="Your Mobile Number.!" cssClass="form-control text-center" required="required"/>

     <form:label path="email"><h3>Email</h3></form:label>
     <form:input type="email" path="email" placeholder="Your Email Id..!" cssClass="form-control text-center" required="required"/>
     <br/>
     <input type="submit" value="Generate" class="btn btn-success form-control"/>
</form:form>

我自己jsp

<div style="margin-top: 3%; font-size: 20px;">
    <h3>My Introduction.</h3>
       <p>
        Hi, I am ${name} my age is ${age} and I am from ${area}. I am working as a ${designation}  
        in ${work}. You can contact me in my mobile ${mobile} and You can also shoot mail to 
        ${email}.
       </p>
</div>

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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>SpringWork</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
    </context-param>

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

mvc-dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-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="com.infofaces.spring.form" />
        <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix">
                <value>/WEB-INF/pages/</value>
            </property>
            <property name="suffix">
                <value>.jsp</value>
            </property>
        </bean>

        <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
            <property name="basename" value="com/infofaces/spring/form/MySelf" />
        </bean>

        <mvc:resources mapping="/resources/**" location="/resources/" />

        <mvc:annotation-driven />

</beans>

我的模型名是Myself.java,它有私有变量和该变量的getter、setter方法。这是我的控制器。

Hello控制器。JAVA

package com.infofaces.spring.form;

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

@Controller
public class HelloController {

    @RequestMapping(value = "/display", method = RequestMethod.GET)
    public ModelAndView display() {
        return new ModelAndView("myself", "command", new MySelf());
    }

    @RequestMapping(value="/addDisplay", method = RequestMethod.POST)
    public String addDisplay(@ModelAttribute("command") MySelf myself, ModelMap model) {
        model.addAttribute("name",myself.getName());
        model.addAttribute("age", myself.getAge());
        model.addAttribute("work", myself.getWork());
        model.addAttribute("designation", myself.getDesignation());
        model.addAttribute("mobile", myself.getMobile());
        model.addAttribute("email", myself.getEmail());
        return "myself";
    }
}

完整堆栈跟踪。

type Exception report

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

description The server encountered an internal error that prevented it from fulfilling this request.

exception

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:549)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:465)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
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.<init>(BindStatus.java:141)
    org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:179)
    org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:199)
    org.springframework.web.servlet.tags.form.LabelTag.autogenerateFor(LabelTag.java:130)
    org.springframework.web.servlet.tags.form.LabelTag.resolveFor(LabelTag.java:120)
    org.springframework.web.servlet.tags.form.LabelTag.writeTagContent(LabelTag.java:90)
    org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:103)
    org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:80)
    org.apache.jsp.index_jsp._jspx_meth_form_005flabel_005f0(index_jsp.java:265)
    org.apache.jsp.index_jsp._jspx_meth_form_005fform_005f0(index_jsp.java:170)
    org.apache.jsp.index_jsp._jspService(index_jsp.java:105)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

请帮助查找我的代码中的问题。提前谢谢。

共有3个答案

许胡非
2023-03-14

您已经在索引中使用了spring表单标记。jsp,其中command/modeldattribute对象应作为绑定表单数据的请求参数,这是您尚未完成的操作,异常消息是它所说的。

要解决此问题,请遵循以下步骤:

步骤1:从web中删除所有欢迎文件列表。xml。

步骤2:将“/”添加到显示获取处理程序中。

@RequestMapping(value = {"/", "/display"}, method = RequestMethod.GET)
 public ModelAndView display() {
    return new ModelAndView("myself", "command", new MySelf());
 }

步骤3:在表单标记中添加modelAttribute名称:

<form:form action="${pageContext.request.contextPath}/addDisplay" 
           method="POST" 
           modelAttribute="command">
何聪
2023-03-14

而不是转发到您的索引。jsp通过欢迎文件列表,您应该添加

<mvc:view-controller path="/" view-name="index"/>

到您的mvc配置,以及伴随的控制器映射,您将在其中向模型添加命令对象,例如。

@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Model model) {
    model.addAttribute("command", new MySelf()); 
    return "index";
}

您的问题是,您通过欢迎文件被转发到index.jsp,并且请求在Spring之前没有处理。然而,您使用Spring的form: form,如果没有通过命令名或模型属性重命名,则需要在请求属性中带有键命令的对象

江渊
2023-03-14

索引中缺少commandName=“command”。jsp文件。

<form:form action="/addDisplay" method="POST" commandName="command" >

在索引之前,确保请求属性中的command对象可用。正在处理jsp。我希望这能奏效。

编辑:正如你在评论中提到的,当你调用索引时。jsp最终会得到java。lang.IllegalStateException:bean名称“command”的BindingResult和普通目标对象都不能作为请求属性使用错误。

因为当您的jsp被呈现时,命令对象首先不可用,您必须向控制器发出请求,将对象放入模型名itcommand,然后提供视图名索引。jsp

例如:

  @RequestMapping(value = "/index", method = RequestMethod.GET)
public ModelAndView display() {
    return new ModelAndView("index", "command", new MySelf());
}

现在你不会得到那个错误。我希望这能奏效。

 类似资料: