java.lang.IllegalStateException:BindingResult和bean名称'User'的普通目标对象都不能用作具有根本原因的请求属性

时间:2013-09-05 18:40:03

标签: spring spring-mvc

我是春天的新手,我在春季MVC中尝试了一个例子,但我收到了上述错误,请检查我的代码并帮助我解决问题。     我尝试了不同的URL映射(如beanNameUrlMapping,simpleUrlMapping)但无法解决问题,请让我知道此问题的根本原因以及解决问题的步骤

先谢谢

我的代码

在此示例中,我只是在jSP中输入注册详细信息,并希望在注册完成后在用户详细信息中显示详细信息。

我的jsp页面输入详细信息
Register.jsp

<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
    <title>Spring 3 MVC Series - Contact Manager</title>
</head>
<body>
<form:form method="post" action="addContact.html" commandName="User"  >

    <table>
    <tr>
        <td><form:label path="firstname">First Name</form:label></td>
        <td><form:input path="firstname" /></td> 
    </tr>
    <tr>
        <td><form:label path="lastname">Last Name</form:label></td>
        <td><form:input path="lastname" /></td>
    </tr>
    <tr>
        <td><form:label path="email">Email</form:label></td>
        <td><form:input path="email" /></td>
    </tr>
    <tr>
        <td><form:label path="telephone">Telephone</form:label></td>
        <td><form:input path="telephone" /></td>
    </tr>
    <tr>
        <td colspan="2">
            <input type="submit" value="Add Contact"/>
        </td>
    </tr>
</table>    

</form:form>
</body>
</html>

在配置文件中将请求映射到控制器     config.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="com.mys" />

    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>



    <bean id="userService" class="com.mys.services.UserServiceImpl" />

    <bean name="/addContact.html" class="com.mys.login.RegistrationController" 
    p:userService-ref="userService" p:formView="Register" p:successView="success" />

</beans>

我的控制器类

import com.formbean.User;
import com.mys.services.UserService;

@SuppressWarnings("deprecation")
public class RegistrationController extends SimpleFormController {

    private UserService userService;

    public RegistrationController() {
        setCommandClass(User.class);
        setCommandName("user");
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @Override
    protected ModelAndView onSubmit(Object command) throws Exception {
        User user = (User) command;
        userService.add(user);
        return new ModelAndView("success", "user", user);
    }

}

Form bean  User.java

package com.formbean;

public class User {
    private String firstname;
    private String lastname;
    private String email;
    private String telephone;

    //setters , getter ()

}

package com.mys.services;

import com.formbean.User;

public class UserServiceImpl implements UserService {

    @Override
    public void add(User user) {
        // TODO Auto-generated method stub
        System.out.println("user added successfully");
    }   
}

这是我得到的问题

严重:BindingResult既不是bean名称也不是普通的目标对象&#39; User&#39;可用作请求属性 java.lang.IllegalStateException:BindingResult和bean名称的普通目标对象&#39; User&#39;可用作请求属性     在org.springframework.web.servlet.support.BindStatus。(BindStatus.java:141)     在org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:174)     在org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:194)     在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.Register_jsp._jspx_meth_form_005flabel_005f0(Register_jsp.java:188)     at org.apache.jsp.Register_jsp._jspx_meth_form_005fform_005f0(Register_jsp.java:112)     在org.apache.jsp.Register_jsp._jspService(Register_jsp.java:71)     在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:390)     在org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:722)     在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)     在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)     在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)     在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)     在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)     在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)     at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)     在org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:563)     在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)     在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:399)     在org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:317)     在org.apache.coyote.http11.Http11Protocol $ Http11ConnectionHandler.process(Http11Protocol.java:204)     在org.apache.coyote.http11.Http11Protocol $ Http11ConnectionHandler.process(Http11Protocol.java:182)     at org.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java:311)     at java.util.concurrent.ThreadPoolExecutor $ Worker.runTask(Unknown Source)     at java.util.concurrent.ThreadPoolExecutor $ Worker.run(Unknown Source)     在java.lang.Thread.run(未知来源) 2013年9月4日下午11:52:13 org.apache.catalina.core.StandardWrapperValve调用 严重:servlet [jsp]的Servlet.service()在路径[/ MysteryShopping]的上下文中引发异常[java.lang.IllegalStateException:BindingResult既不是bean名称也不是普通的目标对象&#39; User&#39;根据原因提供请求属性] java.lang.IllegalStateException:BindingResult和bean名称的普通目标对象&#39; User&#39;可用作请求属性     在org.springframework.web.servlet.support.BindStatus。(BindStatus.java:141)     在org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:174)     在org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:194)     在org.springframework.web.servlet.tags.form.LabelTag.autogenerateFor(LabelTag.java:129)     在org.springframework.web.servlet.tags.form.LabelTag.resolveFor(LabelTag.java:119)

0 个答案:

没有答案