java.lang.IllegalStateException:BeanResult'command'的BindingResult和普通目标对象都不能用作请求属性 [英] java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'command' available as request attribute

查看:75
本文介绍了java.lang.IllegalStateException:BeanResult'command'的BindingResult和普通目标对象都不能用作请求属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Spring MVC Framework的初学者.两天后我开始学习Spring.出于学习目的,我正在开发一个简单的应用程序.即从表单获取用户输入并在另一页中显示值.我得到了一个异常"java.lang.IllegalStateException:BeanResult'command'的BindingResult或普通目标对象都不能用作请求属性".我无法弄清楚代码中出了什么问题.我搜索了Google并尝试了许多解决方案,但问题仍然存在.

I am beginner to Spring MVC Framework. I started to learn Spring two days back. For learning purpose I am developing one simple Application. i.e., Get user input from form and display values in another page. I got an Exception " java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'command' available as request attribute". I cant figure out what's wrong in my code. I searched Google and tried many solution but the problem is still here.

这是我的看法 index.jsp

Here is my view index.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>

myself.jsp

myself.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

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方法.这是我的控制器.

My model name is Myself.java and it has private variables and getter, setter methods for that variable. Here is my controller.

HelloController.java

HelloController.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)

请帮助查找我的代码中的问题.预先感谢.

Please help to find problem in my code. Thanks in advance.

推荐答案

您在index.jsp文件中缺少commandName="command".

You are missing commandName="command" in your index.jsp file .

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

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

Make sure that command object is available in your request attribute before index.jsp is being processed. I hope this would work.

正如您在评论中说的那样,当您完全调用index.jsp时,会出现java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'command' available as request attribute错误.

EDIT : As you said in comment when you call index.jsp definatily you will get java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'command' available as request attribute error.

因为正在呈现您的jsp命令对象时,首先您必须向控制器发出请求,将对象放入模型名称中command,然后提供视图名称index.jsp

Because when your jsp is being rendered command object not available for that first you have to make request to controller , put object into Model name it command and then provide view name index.jsp

例如:

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

现在您将不会收到该错误.我希望这能奏效.

Now you won't get that error. I hope this would work.

这篇关于java.lang.IllegalStateException:BeanResult'command'的BindingResult和普通目标对象都不能用作请求属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆