开发者

Spring 3 MVC : problem with form:errors and bindingresult

I want to validate my inputs but i can't make it work : nothing appear on the page. My project is in java 5, so no JSR303 (@Valid). My only solution, if i'm not mistaken, is to use BindingResult.

My controller 开发者_JAVA技巧:

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

Thanks !


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

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

Also there are no problems with JSR-303 with Java 5. You need JSR-303 provider and API libraries in the classpath, as well as in Java 6.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜