HTTP status returned by Spring MVC for a form POST that fails validation -


if have spring-mvc @controller html form and

  1. you provide validator command object of form
  2. a form post-ed values fail validation using validator
  3. so bindingresult given request handler haserrors().
  4. 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

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -