form:拒绝值时,错误不显示错误消息

时间:2012-11-20 11:08:19

标签: java spring validation spring-mvc html-form

完整的代码在github中: https://github.com/cuipengfei/MPJSP/tree/master/tmp

在控制器中,有一个处理提交的方法:

@RequestMapping(value = "/home", method = RequestMethod.POST)
    public void handleSubmit(Customer model, BindingResult result) {
        System.out.println(model.getUserName());
        result.rejectValue("userName", "required.userName", "user name invalid");
    }

在jsp中,有一个这样的表单:

<form:form method="POST" action="home" modelAttribute="Customer">

    <table>
        <tr>
            <td>Username :</td>
            <td><form:input path="userName" /></td>
            <td><form:errors path="userName" cssClass="error" /></td>
        </tr>
        <tr>
            <td colspan="3"><input type="submit" /></td>
        </tr>
    </table>
</form:form>

控制器每次都拒绝值,但不显示错误消息。

完整的代码可以在这里找到: https://github.com/cuipengfei/MPJSP/tree/master/tmp

1 个答案:

答案 0 :(得分:0)

尝试在表单标记中设置commandName属性

<form:form method="POST" action="home" commandName="Customer">