SimpleFormController - java.lang.IllegalStateException:对于 bean 名称“command”既不是 BindingResult 也不是普通目标对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12123735/
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 07:40:01  来源:igfitidea点击:

SimpleFormController - java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'command'

javaspring-mvc

提问by Sajith

In my basic spring mvc application I am getting the exception. I am using SimpleFormController. My code is as below.

在我的基本 spring mvc 应用程序中,我遇到了异常。我正在使用SimpleFormController. 我的代码如下。

In my dispatcher-servlet

在我的调度程序小服务程序中

<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <props>
            <prop key="index.htm">indexController</prop>
           <!-- <prop key="login.htm">loginController</prop>  -->
        </props>
    </property>
</bean>

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

<!--
The index controller.
-->
<bean name="indexController"
      class="org.springframework.web.servlet.mvc.ParameterizableViewController"
      p:viewName="index" />
<!--  <bean name="loginController"
      class="org.springframework.web.servlet.mvc.ParameterizableViewController"
      p:viewName="login"/>  -->

 <bean name="/login.htm" class="com.pack.controller.LoginController" >
 <property name="commandClass" value="com.pack.model.User" />
 <property name="formView" value="login"/>
 <property name="successView" value="index.htm"/>
 </bean>

In my JSP page

在我的 JSP 页面中

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Login</title>
    </head>
    <body>
        <h1>Login</h1>
        <form:form name="loginForm" method="post">
        ${msg}
        <form:input id="username" path="username"/>
        <form:password id="password" path="password"/>
        <input type="submit" value="login" name="login" />
        </form:form>
    </body>
</html>

In my controller class

在我的控制器类中

public class LoginController extends SimpleFormController{

    @Override
    public ModelAndView onSubmit(Object command) throws ServletException {
        User user =  (User)command;
        System.out.println("Username:" + user.getUsername() + " Password:" + user.getPassword());
        if(user.getUsername().equalsIgnoreCase("sa") && user.getPassword().equalsIgnoreCase("12")){
            return new ModelAndView(new RedirectView(getSuccessView()));
        }
        else
        {
            ModelAndView modelAndView = new ModelAndView("login");
            modelAndView.addObject("msg", "Invalid login");
            return  modelAndView;
        }
    }    
}

Once the user enter correct username and password will redirect to the indexpage. This is working properly. But the user enter wrong login credentials it throws this error.

用户输入正确的用户名和密码后,将重定向到该index页面。这工作正常。但是用户输入错误的登录凭据会引发此错误。

exception
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'command' available as request attribute
root cause
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'command' available as request attribute

异常
org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常是 java.lang.IllegalStateException:Bean 名称“command”的 BindingResult 和普通目标对象均不可用作请求属性
根本原因
java.lang.IllegalStateException:Bean 名称“command”的 BindingResult 或普通目标对象均不可用作请求属性

Any suggestion to solve this issue?

有什么建议可以解决这个问题吗?

采纳答案by Sajith

I got a solution for this problem,If we use HTML controls rather than spring controls, this issue will solve.Here in JSP file,

我得到了这个问题的解决方案,如果我们使用 HTML 控件而不是 spring 控件,这个问题将解决。在 JSP 文件中,

 <%--   <form:input id="username" path="username"/>
        <form:password id="password" path="password"/> --%>
        <input type="text" name="username"/> 
        <input type="password" name="password"/>

This will solve the issue.Thanks for all responses.

这将解决问题。感谢所有回复。

回答by alfredaday

You can reference the spring form tag documentation here: http://static.springsource.org/spring/docs/current/spring-framework-reference/html/spring-form.tld.html#spring-form.tld.form

您可以在此处参考 spring 表单标签文档:http: //static.springsource.org/spring/docs/current/spring-framework-reference/html/spring-form.tld.html#spring-form.tld.form

The culprit is that you are not setting the 'modelAttribute' attribute, which gets defaulted to 'command'; hence, your error message. You should set 'modelAttribute' to the name of the attribute in your model which represents a User.

罪魁祸首是您没有设置“modelAttribute”属性,该属性默认为“command”;因此,您的错误消息。您应该将 'modelAttribute' 设置为模型中代表用户的属性的名称。

Also, I agree with micha that you should use the newer annotation based controllers.

另外,我同意 micha 的观点,您应该使用较新的基于注解的控制器。

Below you will find an example of how to properly use modelAttribute. Notice that when you a request gets routed to users/new, the Controller places an empty user (new User()) into the model under the 'user' key. The JSP then specifies 'user' as the modelAttribute (I'm fairly sure this is case sensitive).

您将在下面找到如何正确使用 modelAttribute 的示例。请注意,当您将请求路由到 users/new 时,Controller 会将一个空用户 (new User()) 放入模型中的“user”键下。然后 JSP 将“用户”指定为 modelAttribute(我很确定这是区分大小写的)。

@Controller
@RequestMapping(value = "/users")
public class UserController {

    @RequestMapping(value = "/new", method = RequestMethod.GET)
    public String newForm(Model model) {
        model.addAttribute("user", new User());

        return "users/new";
    }

    @RequestMapping(method = RequestMethod.POST)
    public String create(User user) {
        this.userRepository.save(user);

        return "redirect:/users/" + user.getId();
    }

}

Inside "users/new.jsp":

在“users/new.jsp”中:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="sf" %>
<html>

<body>
<c:url value="/users" var="users"/>

<sf:form action="${users}" method="POST" modelAttribute="user">
    <table>
        <tr>
            <th><label for="full_name">Full Name:</label></th>
            <td><sf:input path="name" size="15" id="full_name" /></td>
        </tr>
    </table>
    <input type="submit" value="Create User" />
</sf:form>

回答by micha

Maybe not a real answer to your problem but you should use (newer) annotation based controllers if possible. You are using SimpleFormControllerwhich is deprecated in the current spring version.

也许不是您问题的真正答案,但如果可能,您应该使用(较新的)基于注释的控制器。您正在使用SimpleFormController哪个在当前 spring 版本中已弃用。

Check thisor thisfor an alternative.

检查以获取替代方法。

回答by A'n' user

This is not a valid solution, rather a work-around, as the binding to your command object would not be available.

这不是一个有效的解决方案,而是一种变通方法,因为绑定到您的命令对象将不可用。

In a similar error situation, I found out that the commandName in my *-servlet.xml was differrent. When I made it identical to the actual commandBean, the error had disappeared.

在类似的错误情况下,我发现 *-servlet.xml 中的 commandName 是不同的。当我使它与实际的 commandBean 相同时,错误就消失了。