commandName的名称应该始终是Bean的名称吗?

时间:2014-04-05 12:13:37

标签: java spring spring-mvc

这是我的客户JSP页面,其中commandName以“customerForm”:

的形式给出
<form:form method="POST" commandName="customerForm">
<table>
<tr>
<td>UserName : </td>
<td><form:input path="userName" /></td>
<td><form:errors path="userName" cssClass="error" /></td>
</tr>
<tr>
<td>Password : </td>
<td><form:password path="password" /></td>
<td><form:errors path="password" cssClass="error" /></td>
</tr>
</table>
</form:form>

这是我使用客户POJO的Controller类:

@RequestMapping(method = RequestMethod.POST)
    public String processSubmit(
            @ModelAttribute("customerForm") Customer customer,
            BindingResult result, SessionStatus status) {

        customerValidator.validate(customer, result);

        if (result.hasErrors()) {
            //if validator failed
            return "CustomerForm";
        } else {
            status.setComplete();
            //form success
            return "CustomerSuccess";
        }
    }

    @RequestMapping(method = RequestMethod.GET)
    public String szdfsdf(ModelMap model){

        Customer cust = new Customer();
                model.addAttribute("customerForm", cust);

        //return form view
        return "CustomerForm";
    }

得到的错误是:

SEVERE: Servlet.service() for servlet jsp threw exception
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'customerForm' available as request attribute

当我给CommandName =“customer”时它工作正常。即使ModelAttribute和CommandName的名称不同。

0 个答案:

没有答案