简体   繁体   中英

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).

Important parts of the code:

My 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:

@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.

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 () ?

AFAIK, you have to define explicitly what you want to do in case of validation errors. @Valid annotation will validate your model attribute but you have to mwention explicitly in controller what the beaviour after error using binding result

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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