Spring 3 MVC: form issue: errors and bindingresult

I want to check my inputs, but I cannot get it to work: nothing is displayed on the page. My project is in java 5, so there is no JSR303 (@Valid). My only solution, if I'm not mistaken, is to use BindingResult.

My controller:

@Controller
public class MyController {

    @RequestMapping(method = RequestMethod.POST, value = "myPage.html")
    public void myHandler(MyForm myForm, BindingResult result, Model model) {
        result.reject("field1", "error message 1");
    }
}

My jsp:

<form:form commandName="myForm" method="post">
    <label>Field 1 : </label>
    <form:input path="field1" />
    <form:errors path="field1" />

    <input type="submit" value="Post" />
</form:form>

What am I missing?

Thank!

+3
source share
1 answer

BindingResult.reject()associates the error message with the form as a whole, it can be displayed <form:errors/>without path. To associate an error with a specific form field, use BindingResult.rejectValue():

 result.rejectValue("field1", "messageCode", "Default error message"); 

There is also no problem with JSR-303 with Java 5. You need the JSR-303 provider and API libraries in the classpath, as well as in Java 6.

+12
source

Source: https://habr.com/ru/post/1784988/


All Articles