** BindingResult既不是bean名称的普通目标对象,也不是用户'可用作请求属性**

时间:2014-10-22 06:54:00

标签: spring model-view-controller

我正在尝试使用spring mvc读取从表单输入的值到另一个jsp页面但不使用注释。虽然这样做我得到这个错误:    BindingResult既不是bean名称的普通目标对象,也不是用户'可用作请求属性 我的控制器类代码如下:

public class UserFormController extends SimpleFormController {


    public UserFormController()
    {
        setCommandClass(User.class);

    }

     protected ModelAndView onSubmit(Object command) throws Exception {

        User user = (User) command;

        return new ModelAndView("userDetails", "user", user);

    }

}

我的POJO代码是:

public class User {
    String name;
    String gender;
    String[] subject;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
    public String[] getSubject() {
        return subject;
    }
    public void setSubject(String[] subject) {
        this.subject = subject;
    }
}

表格的代码是:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form:form method="POST" commandName="user" action="/abc.htm">
<table>
<tr>
     <form:input path="name" /> 
</tr>
<tr>
    <form:radiobutton label="M" value="male" path="gender"/>
    <form:radiobutton label="F" value="female" path="gender"/>
</tr>
<tr>
    <form:checkbox label="maths" value="maths" path="subject"/>
    <form:checkbox label="english" value="english" path="subject"/>
</tr>
</table>
<input type="submit" value="submit">
</form:form>
</body>
</html>

我的欢迎页面是redirect.jsp,从我重定向到redirection.jsp并使用web.xml我将控件发送到名为&#34; disp&#34;的servlet。 &#34; disp-servlet.xml&#34;的代码是:

<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
    p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/>

    <bean name="/redirection.htm" 
            class="org.springframework.web.servlet.mvc.ParameterizableViewController">
        <property name="viewName" value="entry" />
   </bean>


<bean name="/abc.htm" class="UserFormController"/>
</beans>

通过&#39; ParameterizableViewController&#39;我正在尝试将请求重定向到WEB-INF / jsp文件夹中的entry.jsp。然后我试图在&#34; userDetails.jsp&#34;上显示细节:

<%@ 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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<table>
<tr>
user name:${user.name} 
</tr>
<tr>
user gender:${user.gender}
</tr>
<tr>
user subjects:${user.subjects[0]},${user.subjects[1]}
</tr>
</table>
</body>
</html> 

我得到的错误是:

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'user' available as request attribute
org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:141)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:175)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:195)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:161)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.autogenerateId(AbstractDataBoundFormElementTag.java:148)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.resolveId(AbstractDataBoundFormElementTag.java:139)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.writeDefaultAttributes(AbstractDataBoundFormElementTag.java:123)
org.springframework.web.servlet.tags.form.AbstractHtmlElementTag.writeDefaultAttributes(AbstractHtmlElementTag.java:409)
org.springframework.web.servlet.tags.form.InputTag.writeTagContent(InputTag.java:140)
org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:91)
org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:78)
org.apache.jsp.WEB_002dINF.jsp.entry_jsp._jspx_meth_form_005finput_005f0(entry_jsp.java:184)
org.apache.jsp.WEB_002dINF.jsp.entry_jsp._jspx_meth_form_005fform_005f0(entry_jsp.java:125)
org.apache.jsp.WEB_002dINF.jsp.entry_jsp._jspService(entry_jsp.java:84)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:239)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1060)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:798)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:647)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:552)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

1 个答案:

答案 0 :(得分:1)

根据堆栈跟踪,错误发生在名为entry.jsp的视图的呈现中。我假设它是您在上面显示的表单。

标记<form:form method="POST" commandName="user" action="/abc.htm">搜索名称为user的请求属性(正常),并且找不到(因此错误)。

正常处理的表单如下:

  • 浏览器向应用程序发出GET请求
  • 应用程序生成并发送包含表单
  • 的HTML页面
  • 浏览器发出带有表单数据的POST请求
  • 应用程序处理表单数据并重定向到另一个URL

您的错误发生在第2阶段:应用程序生成HTML页面,因为此时您没有请求属性。

你应该做什么:

  • 忘记弃用(从3.0开始)SimpleFormController并使用明确更简单且功能更强大的带注释的控制器 - 您可以找到许多示例
  • 如果你不能(Spring 2.5或java 1.4但非常旧且未经过维护),请使用 UserFormController通过实施showForm方法或至少将formView设置为entry.jsp并实施referenceData方法返回地图{{1}来准备视图}}

修改

带注释的控制器可能如下所示:

{ "user" : new User() }