代码之家  ›  专栏  ›  技术社区  ›  Oussama

BindingResult和bean名称“command”的普通目标对象都不能作为请求属性使用-属性错误

  •  0
  • Oussama  · 技术社区  · 7 年前

    我是按照一个教程,我得到了这个错误

    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名称“command” 因此,我将属性的名称改为command,它工作了,但是如果使用customer,为什么它不工作呢。 在教程中,他没有使用命令。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Gurkan Yesilyurt    7 年前

    尝试使用camel case约定 modelAttribute="customer" . 否则,预计将是 command -默认情况下命名模型,即。 model.addAttribute("command", customer);