简体   繁体   English

Spring Form对象验证不会产生错误消息

[英]Spring Form object validation doesn't produce error messages

I'm trying to make Spring validate the submitted table, but after submit it only reloads the portlet (that's ok, but it should display error messages). 我试图让Spring验证提交的表,但是在提交后,它仅重新加载portlet(没关系,但是它应该显示错误消息)。

Important parts of the code: 该代码的重要部分:

My JSP 我的JSP

<form:form method="post" action="${submitFormLink}" modelAttribute="formObject" commandName="formObject" enctype="multipart/form-data">
    <form:errors path="*" cssClass="errorblock" element="div" />
    <table>
        <tr>
            <td>
                <form:input type="text" path="someVar"/>
            </td>
            <td>
                <form:errors path="someVar" cssClass="error" />
            </td>
        </tr>
    </table>
</form:form>

My Controller 我的控制器

@RequestMapping
public String create(Model model) throws IOException {
    model.addAttribute("formObject", new FormObject());
    return "view";
}

@ActionMapping(params = {"action=submit"})
public void submit(ActionRequest request, @Valid @ModelAttribute("formObject") FormObject formObject,
                   ActionResponse response) throws IOException, RepositoryException,
                    PortalException, SystemException {
    // some unimportant stuff
    // I want to validate automatically not by calling BindingResult
}

I have also included 我也包括了

<mvc:annotation-driven />

in my foo-context.xml and set a constraint to one of the FormObject variables like this: 在我的foo-context.xml中,并为这样的FormObject变量之一设置约束:

@Size(min = 1, max = 35, message = "Enter between 1-35 characters.")
private String someVar;

My error blocks do not get filled with errors after submission, they just stay as an empty <td> as if validation was successful. 提交后,我的错误块没有被错误填充,它们只是作为空白<td>保留,就好像验证成功一样。

Can anyone please tell me what did I miss regarding configuration, or where I'm doing something wrong? 谁能告诉我我在配置方面错过了什么,或者我在哪里做错了什么? Thank you! 谢谢!

When you say // I want to validate automatically not by calling BindingResult , I assume you mean you dont want to use bindingResult.hasErrors () ? 当您说// I want to validate automatically not by calling BindingResult ,我假设您是说您不想使用bindingResult.hasErrors()吗?

AFAIK, you have to define explicitly what you want to do in case of validation errors. AFAIK,您必须明确定义在发生验证错误时要执行的操作。 @Valid annotation will validate your model attribute but you have to mwention explicitly in controller what the beaviour after error using binding result @Valid批注将验证您的模型属性,但您必须使用绑定结果在控制器中明确修改错误后的行为

if (bindingResult.hasErrors())
        return "someView";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM