HTTP status returned by Spring MVC for a form POST that fails validation -
if have spring-mvc @controller html form and
- you provide
validatorcommand object of form - a form post-ed values fail validation using validator
- so
bindingresultgiven request handlerhaserrors(). - your request handler returns form view-name view name use, user can see error message(s) , submit corrected form
what http status code reply client have? 400 (bad request), might expect restful service? is, spring examine bindingresult , use presence of errors set status code?
sample code:
@controller public class mycontroller { public static class command { ... } public static final string command = "command"; public static final string form_view = "view"; private validator formvalidator = ... @requestmapping(value = { "myform" }, method = requestmethod.post) public string processaddcheckform( @modelattribute(value = command) @valid final command command, final bindingresult bindingresult) { if (!bindingresult.haserrors()) { // processing of valid form return "redirect:"+uri; } else { return form_view; // status code spring set after return? } } @initbinder(value = command) protected void initaddcheck(webdatabinder binder) { binder.addvalidators(formvalidator); } }
the status code is, perhaps surprisingly, not 400 (bad request), 200 (ok).
this presumably ensure user's web browser display form error messages, rather generic 400 (bad request) error page. however, firefox seems ok if have request handler sets status code 400 case.
Comments
Post a Comment