BeanResult'command'的BindingResult或普通目标对象都不能用作请求属性-属性错误

时间:2018-10-13 15:58:18

标签: spring-mvc

我正在学习教程,但出现此错误

Neither BindingResult nor plain target object for bean name 'command' available as request attribute

这是我正在使用的代码。

<form:form action="saveCustomer" ModelAttribute="customer" method="POST">
            <table>
                <tbody>
                    <tr>
                        <td><label>First name:</label></td>
                        <td><form:input path="firstName"/></td>
                    </tr>
                    <tr>
                        <td><label>Last name:</label></td>
                        <td><form:input path="lastName"/></td>
                    </tr>
                    <tr>
                        <td><label>Email:</label></td>
                        <td><form:input path="email"/></td>
                    </tr>   
                </tbody>
            </table>
        </form:form>

这是添加新客户的客户表格,问题出现在名字输入

显示表单的get方法是:

@GetMapping("/showFormForAdd")
    public String showFormForAdd(Model theModel) {
        Customer theCustomer = new Customer();
        theModel.addAttribute("customer", theCustomer);
        return "customer-form";
    }

,客户实体为:

@Column(name="first_name")
private String firstName;

@Column(name="last_name")
private String lastName;

@Column(name="email")
private String email;

在错误中显示 bean name'command',因此我将属性的名称更改为command并起作用,但是如果我使用客户,为什么它不起作用。 在本教程中,他没有使用命令。

1 个答案:

答案 0 :(得分:1)

尝试使用驼峰式约定modelAttribute="customer"。否则,默认情况下应该使用command命名模型,即model.addAttribute("command", customer);,您需要将其作为模型发送。